private string HashText(string text, string salt, System.Security.Cryptography.HashAlgorithm hash) { byte[] textWithSaltBytes = Encoding.UTF8.GetBytes(string.Concat(text, salt)); byte[] hashedBytes = hash.ComputeHash(textWithSaltBytes); hash.Clear(); return(Convert.ToBase64String(hashedBytes)); }
private static string HashText(string text, string salt, HashAlgorithm hasher) { var hashedText = Convert.ToBase64String( hasher.ComputeHash( Encoding.UTF8.GetBytes(string.Concat(text, salt)) ) ); hasher.Clear(); return hashedText; }
/// <summary> /// Disposes hashing algorithm. /// </summary> /// <param name="hashAlgorithm">The hash algorithm to dispose.</param> private static void DisposeAlgorithm(HashAlgorithm hashAlgorithm) { hashAlgorithm.Clear(); }
byte[] Crypt(byte[] key, byte[] salt, byte[] prefix, HashAlgorithm A) { byte[] H = null, I = null; try { A.Initialize(); AddToDigest(A, key); AddToDigest(A, salt); AddToDigest(A, key); FinishDigest(A); I = (byte[])A.Hash.Clone(); A.Initialize(); AddToDigest(A, key); AddToDigest(A, prefix); AddToDigest(A, salt); AddToDigestRolling(A, I, 0, I.Length, key.Length); int length = key.Length; for (int i = 0; i < 31 && length != 0; i++) { AddToDigest(A, new[] { (length & (1 << i)) != 0 ? (byte)0 : key[0] }); length &= ~(1 << i); } FinishDigest(A); H = (byte[])A.Hash.Clone(); for (int i = 0; i < 1000; i++) { A.Initialize(); if ((i & 1) != 0) { AddToDigest(A, key); } if ((i & 1) == 0) { AddToDigest(A, H); } if ((i % 3) != 0) { AddToDigest(A, salt); } if ((i % 7) != 0) { AddToDigest(A, key); } if ((i & 1) != 0) { AddToDigest(A, H); } if ((i & 1) == 0) { AddToDigest(A, key); } FinishDigest(A); Array.Copy(A.Hash, H, H.Length); } byte[] crypt = new byte[H.Length]; int[] permutation = new[] { 11, 4, 10, 5, 3, 9, 15, 2, 8, 14, 1, 7, 13, 0, 6, 12 }; Array.Reverse(permutation); for (int i = 0; i < crypt.Length; i++) { crypt[i] = H[permutation[i]]; } return crypt; } finally { A.Clear(); Security.Clear(H); Security.Clear(I); } }
/// <summary> Mechanisms inherit from the HashAlgorithm base class so we can use that to cast the crypto service provider /// </summary> /// <param name="provider">Type of provider</param> /// <param name="plainText">To be hash content</param> /// <returns>Array of bytes</returns> private byte[] ComputeHash(HashAlgorithm provider, string plainText) { //All hashing mechanisms inherit from the HashAlgorithm base class so we can use that to cast the crypto service provider byte[] hash = provider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(plainText)); provider.Clear(); return hash; }
static string UnsaltedCrypt(HashAlgorithm algorithm, byte[] password) { try { algorithm.Initialize(); algorithm.TransformBlock(password, 0, password.Length, password, 0); algorithm.TransformFinalBlock(new byte[0], 0, 0); string crypt = Convert.ToBase64String(algorithm.Hash); return crypt; } finally { algorithm.Clear(); } }
static string SaltedCrypt(HashAlgorithm algorithm, byte[] password, byte[] salt) { // If we're under the hash length, assume we only have the salt. int hashLength = algorithm.HashSize / 8; int saltOffset = salt.Length < hashLength ? 0 : hashLength; int saltLength = salt.Length - saltOffset; byte[] saltedHash = new byte[hashLength + saltLength]; try { algorithm.Initialize(); algorithm.TransformBlock(password, 0, password.Length, password, 0); algorithm.TransformBlock(salt, saltOffset, saltLength, salt, saltOffset); algorithm.TransformFinalBlock(new byte[0], 0, 0); Array.Copy(algorithm.Hash, saltedHash, hashLength); Array.Copy(salt, saltOffset, saltedHash, hashLength, saltLength); string crypt = Convert.ToBase64String(saltedHash); return crypt; } finally { algorithm.Clear(); Security.Clear(saltedHash); } }
byte[] Crypt(byte[] key, byte[] salt, int rounds, HashAlgorithm A) { byte[] P = null, S = null, H = null, I = null; try { A.Initialize(); AddToDigest(A, key); AddToDigest(A, salt); AddToDigest(A, key); FinishDigest(A); I = (byte[])A.Hash.Clone(); A.Initialize(); AddToDigest(A, key); AddToDigest(A, salt); AddToDigestRolling(A, I, 0, I.Length, key.Length); int length = key.Length; for (int i = 0; i < 31 && length != 0; i++) { AddToDigest(A, (length & (1 << i)) != 0 ? I : key); length &= ~(1 << i); } FinishDigest(A); H = (byte[])A.Hash.Clone(); A.Initialize(); for (int i = 0; i < key.Length; i++) { AddToDigest(A, key); } FinishDigest(A); P = new byte[key.Length]; CopyRolling(A.Hash, 0, A.Hash.Length, P); A.Initialize(); for (int i = 0; i < 16 + H[0]; i++) { AddToDigest(A, salt); } FinishDigest(A); S = new byte[salt.Length]; CopyRolling(A.Hash, 0, A.Hash.Length, S); for (int i = 0; i < rounds; i++) { A.Initialize(); if ((i & 1) != 0) { AddToDigest(A, P); } if ((i & 1) == 0) { AddToDigest(A, H); } if ((i % 3) != 0) { AddToDigest(A, S); } if ((i % 7) != 0) { AddToDigest(A, P); } if ((i & 1) != 0) { AddToDigest(A, H); } if ((i & 1) == 0) { AddToDigest(A, P); } FinishDigest(A); Array.Copy(A.Hash, H, H.Length); } byte[] crypt = new byte[H.Length]; int[] permutation = GetCryptPermutation(); for (int i = 0; i < crypt.Length; i++) { crypt[i] = H[permutation[i]]; } return crypt; } finally { A.Clear(); Security.Clear(P); Security.Clear(S); Security.Clear(H); Security.Clear(I); } }
byte[] Crypt(byte[] key, byte[] salt, int rounds, HashAlgorithm A) { byte[] H = null; try { A.Initialize(); AddToDigest(A, salt); AddToDigest(A, key); FinishDigest(A); H = (byte[])A.Hash.Clone(); for (int i = 0; i < (1 << rounds); i++) { A.Initialize(); AddToDigest(A, H); AddToDigest(A, key); FinishDigest(A); Array.Copy(A.Hash, H, H.Length); } return (byte[])H.Clone(); } finally { A.Clear(); Security.Clear(H); } }