Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. public static class CryptographyExtensions
  2. {
  3. /// <summary>
  4. /// Calculates the MD5 hash for the given string.
  5. /// </summary>
  6. /// <returns>A 32 char long MD5 hash.</returns>
  7. public static string GetHashMd5(this string input)
  8. {
  9. return ComputeHash(input, new MD5CryptoServiceProvider());
  10. }
  11.  
  12. /// <summary>
  13. /// Calculates the SHA-1 hash for the given string.
  14. /// </summary>
  15. /// <returns>A 40 char long SHA-1 hash.</returns>
  16. public static string GetHashSha1(this string input)
  17. {
  18. return ComputeHash(input, new SHA1Managed());
  19. }
  20.  
  21. /// <summary>
  22. /// Calculates the SHA-256 hash for the given string.
  23. /// </summary>
  24. /// <returns>A 64 char long SHA-256 hash.</returns>
  25. public static string GetHashSha256(this string input)
  26. {
  27. return ComputeHash(input, new SHA256Managed());
  28. }
  29.  
  30. /// <summary>
  31. /// Calculates the SHA-384 hash for the given string.
  32. /// </summary>
  33. /// <returns>A 96 char long SHA-384 hash.</returns>
  34. public static string GetHashSha384(this string input)
  35. {
  36. return ComputeHash(input, new SHA384Managed());
  37. }
  38.  
  39. /// <summary>
  40. /// Calculates the SHA-512 hash for the given string.
  41. /// </summary>
  42. /// <returns>A 128 char long SHA-512 hash.</returns>
  43. public static string GetHashSha512(this string input)
  44. {
  45. return ComputeHash(input, new SHA512Managed());
  46. }
  47.  
  48. public static string ComputeHash(string input, HashAlgorithm hashProvider)
  49. {
  50. if (input == null)
  51. {
  52. throw new ArgumentNullException("input");
  53. }
  54.  
  55. if (hashProvider == null)
  56. {
  57. throw new ArgumentNullException("hashProvider");
  58. }
  59.  
  60. var inputBytes = Encoding.UTF8.GetBytes(input);
  61. var hashBytes = hashProvider.ComputeHash(inputBytes);
  62. var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
  63.  
  64. return hash;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement