Advertisement
Guest User

C# SHA1 Example

a guest
Sep 12th, 2011
2,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System.Security.Cryptography;
  2.  
  3. /// <summary>
  4. /// Calculates SHA1 hash
  5. /// </summary>
  6. /// <param name="text">input string</param>
  7. /// <param name="enc">Character encoding</param>
  8. /// <returns>SHA1 hash</returns>
  9. public static string CalculateSHA1(string text, Encoding enc)
  10. {
  11.     // Convert the input string to a byte array
  12.     byte[] buffer = enc.GetBytes(text);
  13.  
  14.     // In doing your test, you won't want to re-initialize like this every time you test a
  15.     // string.
  16.     SHA1CryptoServiceProvider cryptoTransformSHA1 =
  17.     new SHA1CryptoServiceProvider();  
  18.  
  19.     // The replace won't be necessary for your tests so long as you are consistent in what
  20.     // you compare.    
  21.     string hash = BitConverter.ToString(
  22.         cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
  23.  
  24.     return hash;
  25. }
  26.  
  27. // Original example without using directive or comments:
  28. http://dotnetpulse.blogspot.com/2007/12/sha1-hash-calculation-in-c.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement