Skip to content

Instantly share code, notes, and snippets.

@sorted-bits
Created October 20, 2011 19:31
Show Gist options
  • Save sorted-bits/1302074 to your computer and use it in GitHub Desktop.
Save sorted-bits/1302074 to your computer and use it in GitHub Desktop.
MD5 and SHA1 hashing
public static string ToSHA1(this string text, Encoding enc)
{
byte[] buffer = enc.GetBytes(text);
SHA1CryptoServiceProvider cryptoTransformSHA1 =
new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(
cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
return hash;
}
public static string ToMD5(this string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment