protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { _hash?.Dispose(); if (_baseValue != null) { Array.Clear(_baseValue, 0, _baseValue.Length); } if (_extra != null) { Array.Clear(_extra, 0, _extra.Length); } if (_password != null) { Array.Clear(_password, 0, _password.Length); } if (_salt != null) { Array.Clear(_salt, 0, _salt.Length); } } }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { _hash?.Dispose(); if (_baseValue != null) { Array.Clear(_baseValue); } if (_extra != null) { Array.Clear(_extra); } if (_password != null) { Array.Clear(_password); } if (_salt != null) { Array.Clear(_salt); } } }
private static string Hash(string original, HashAlgorithm algorithm) { byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(original)); algorithm.Dispose(); StringBuilder sb = new StringBuilder(); foreach (byte b in hash) { sb.Append(b.ToString("x2").ToLowerInvariant()); } return sb.ToString(); }
/// <summary> /// Generate hashed string of the string to according to hash algorithm. /// </summary> /// <param name="source"></param> /// <param name="hashAlgorithm"></param> /// <returns></returns> public static string GenerateHashString(this string source, HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256) { if (string.IsNullOrEmpty(source)) { throw new ArgumentException(nameof(source)); } var sourceData = Encoding.UTF8.GetBytes(source); byte[] hashedData = null; System.Security.Cryptography.HashAlgorithm hash = null; try { switch (hashAlgorithm) { case HashAlgorithm.SHA1: hash = SHA1.Create(); break; case HashAlgorithm.SHA512: { hash = SHA512.Create(); break; } case HashAlgorithm.SHA256: default: { hash = SHA256.Create(); break; } } hashedData = hash.ComputeHash(sourceData); } finally { hash?.Dispose(); } var result = BitConverter.ToString(hashedData).ToLower().Replace("-", ""); return(result); }