Skip to content

Instantly share code, notes, and snippets.

@erikzaadi
Created December 9, 2010 08:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikzaadi/734467 to your computer and use it in GitHub Desktop.
Save erikzaadi/734467 to your computer and use it in GitHub Desktop.
A one liner method for MD5 Hashing / performance / verbose approach?
public static string ToMD5(this string input)
{
//comment about this
var sb = new System.Text.StringBuilder();
//comment about that
var md5 = System.Security.Cryptography.MD5.Create();
var bytesFromInput = System.Text.Encoding.Unicode.GetBytes(input);
var hashedBytes = md5.ComputeHash(bytesFromInput);
foreach (var byt in hashedBytes)
{
//To Hexa comment blah
sb.Append(byt.ToString("X2"));
}
return sb.ToString();
}
public static string CalculateMd5Hash(string input)
{
return string.Join("",
System.Security.Cryptography.MD5.Create()
.ComputeHash(System.Text.Encoding.Unicode.GetBytes(input))
.Select(byt => byt.ToString("X2")));
}
public static string CalculateMd5HashBetterPerhaps(string input)
{
var sb = new System.Text.StringBuilder();
foreach (var byt in System.Security.Cryptography.MD5.Create()
.ComputeHash(System.Text.Encoding.Unicode.GetBytes(input)))
{
sb.Append(byt.ToString("X2"));
}
return sb.ToString();
}
@alexbeletsky
Copy link

Wow) thats so cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment