Created
October 20, 2011 19:31
-
-
Save sorted-bits/1302074 to your computer and use it in GitHub Desktop.
MD5 and SHA1 hashing
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 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