Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string ComputeHash(string plainText, byte[] saltBytes)
- {
- // Convert plain text into a byte array.
- var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
- // Allocate array, which will hold plain text and salt.
- var plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
- // Copy plain text bytes into resulting array.
- for (var i = 0;i < plainTextBytes.Length;i++)
- plainTextWithSaltBytes[i] = plainTextBytes[i];
- // Append salt bytes to the resulting array.
- for (var i = 0;i < saltBytes.Length;i++)
- plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
- // Use the SHA512 encryption
- var hash = new SHA512Managed();
- // Compute hash value of our plain text with appended salt.
- var hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
- // Create array which will hold hash and original salt bytes.
- var hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];
- // Copy hash bytes into resulting array.
- for (var i = 0;i < hashBytes.Length;i++)
- hashWithSaltBytes[i] = hashBytes[i];
- // Append salt bytes to the result.
- for (var i = 0;i < saltBytes.Length;i++)
- hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
- // Convert result into a base64-encoded string.
- var hashValue = Convert.ToBase64String(hashWithSaltBytes);
- return hashValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment