MD5

From Unify Community Wiki
Revision as of 22:02, 23 April 2007 by Matthew (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: Matthew Wegner

Overview

This C# code snippet generates an MD5 hash for an input string. The formatting will match the output of PHP's md5() function.

Code

Best placed in your static-only utility class.

<csharp> public string Md5Sum(string strToEncrypt) { System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding(); byte[] bytes = ue.GetBytes(strToEncrypt);

// encrypt bytes System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hashBytes = md5.ComputeHash(bytes);

// Convert the encrypted bytes back to a string (base 16) string hashString = "";

for (int i = 0; i < hashBytes.Length; i++) { hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0'); }

return hashString.PadLeft(32, '0'); } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox