Skip to content

Instantly share code, notes, and snippets.

@smetronic
Created November 15, 2016 12:14
Show Gist options
  • Save smetronic/7d090019699595e2cfef8d6909361a17 to your computer and use it in GitHub Desktop.
Save smetronic/7d090019699595e2cfef8d6909361a17 to your computer and use it in GitHub Desktop.
Calculate a MD5 hash from a string
// 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