DefconDotNet

ComputeHash

Feb 15th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1.         public static string ComputeHash(string plainText, byte[] saltBytes)
  2.         {
  3.             // Convert plain text into a byte array.
  4.             var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  5.  
  6.             // Allocate array, which will hold plain text and salt.
  7.             var plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
  8.  
  9.             // Copy plain text bytes into resulting array.
  10.             for (var i = 0;i < plainTextBytes.Length;i++)
  11.                 plainTextWithSaltBytes[i] = plainTextBytes[i];
  12.  
  13.             // Append salt bytes to the resulting array.
  14.             for (var i = 0;i < saltBytes.Length;i++)
  15.                 plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
  16.            
  17.             // Use the SHA512 encryption
  18.             var hash = new SHA512Managed();
  19.  
  20.             // Compute hash value of our plain text with appended salt.
  21.             var hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
  22.  
  23.             // Create array which will hold hash and original salt bytes.
  24.             var hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];
  25.  
  26.             // Copy hash bytes into resulting array.
  27.             for (var i = 0;i < hashBytes.Length;i++)
  28.                 hashWithSaltBytes[i] = hashBytes[i];
  29.  
  30.             // Append salt bytes to the result.
  31.             for (var i = 0;i < saltBytes.Length;i++)
  32.                 hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
  33.  
  34.             // Convert result into a base64-encoded string.
  35.             var hashValue = Convert.ToBase64String(hashWithSaltBytes);
  36.  
  37.             return hashValue;
  38.         }
Advertisement
Add Comment
Please, Sign In to add comment