Skip to content

Instantly share code, notes, and snippets.

@rmacfie
Created February 15, 2011 19:21
Show Gist options
  • Save rmacfie/828054 to your computer and use it in GitHub Desktop.
Save rmacfie/828054 to your computer and use it in GitHub Desktop.
Generate Md5 and SHA hashes in C#.NET.
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