Created
November 15, 2016 12:14
-
-
Save smetronic/7d090019699595e2cfef8d6909361a17 to your computer and use it in GitHub Desktop.
Calculate a MD5 hash from a string
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
// given, a password in a string | |
string password = @"1234abcd"; | |
// byte array representation of that string | |
byte[] encodedPassword = new UTF8Encoding().GetBytes(password); | |
// need MD5 to calculate the hash | |
byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword); | |
// string representation (similar to UNIX format) | |
string encoded = BitConverter.ToString(hash) | |
// without dashes | |
.Replace("-", string.Empty) | |
// make lowercase | |
.ToLower(); | |
// encoded contains the hash you are wanting |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment