-
-
Save Van-Dame/b6c0c387d9a0921b466265c3f904e914 to your computer and use it in GitHub Desktop.
Generate Md5 and SHA hashes in C#.NET.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CryptographyExtensions | |
{ | |
/// <summary> | |
/// Calculates the MD5 hash for the given string. | |
/// </summary> | |
/// <returns>A 32 char long MD5 hash.</returns> | |
public static string GetHashMd5(this string input) | |
{ | |
return ComputeHash(input, new MD5CryptoServiceProvider()); | |
} | |
/// <summary> | |
/// Calculates the SHA-1 hash for the given string. | |
/// </summary> | |
/// <returns>A 40 char long SHA-1 hash.</returns> | |
public static string GetHashSha1(this string input) | |
{ | |
return ComputeHash(input, new SHA1Managed()); | |
} | |
/// <summary> | |
/// Calculates the SHA-256 hash for the given string. | |
/// </summary> | |
/// <returns>A 64 char long SHA-256 hash.</returns> | |
public static string GetHashSha256(this string input) | |
{ | |
return ComputeHash(input, new SHA256Managed()); | |
} | |
/// <summary> | |
/// Calculates the SHA-384 hash for the given string. | |
/// </summary> | |
/// <returns>A 96 char long SHA-384 hash.</returns> | |
public static string GetHashSha384(this string input) | |
{ | |
return ComputeHash(input, new SHA384Managed()); | |
} | |
/// <summary> | |
/// Calculates the SHA-512 hash for the given string. | |
/// </summary> | |
/// <returns>A 128 char long SHA-512 hash.</returns> | |
public static string GetHashSha512(this string input) | |
{ | |
return ComputeHash(input, new SHA512Managed()); | |
} | |
public static string ComputeHash(string input, HashAlgorithm hashProvider) | |
{ | |
if (input == null) | |
{ | |
throw new ArgumentNullException("input"); | |
} | |
if (hashProvider == null) | |
{ | |
throw new ArgumentNullException("hashProvider"); | |
} | |
var inputBytes = Encoding.UTF8.GetBytes(input); | |
var hashBytes = hashProvider.ComputeHash(inputBytes); | |
var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty); | |
return hash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment