Created
August 16, 2011 13:55
-
-
Save mfloryan/1149138 to your computer and use it in GitHub Desktop.
MD5 for Strings in C#
This file contains hidden or 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
| static string getMd5Hash(string input) | |
| { | |
| // Create a new instance of the MD5CryptoServiceProvider object. | |
| MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); | |
| // Convert the input string to a byte array and compute the hash. | |
| byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); | |
| // Create a new Stringbuilder to collect the bytes | |
| // and create a string. | |
| StringBuilder sBuilder = new StringBuilder(); | |
| // Loop through each byte of the hashed data | |
| // and format each one as a hexadecimal string. | |
| for (int i = 0; i < data.Length; i++) | |
| { | |
| sBuilder.Append(data[i].ToString("x2")); | |
| } | |
| // Return the hexadecimal string. | |
| return sBuilder.ToString(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment