Warning: Cannot modify header information - headers already sent by (output started at /var/www/lalieno.it/index.php:48) in /var/www/lalieno.it/inc/cookie.php on line 3
a cadenza discontinua
Come se fossi
BLOG

C# - Calculate MD5 of a string

Calculate the MD5 of a string is common practice especially in the preservation of the DB password or configuration file. Below a small function useful for calculating, note that the string is first converted to unicode to have a coding reference.

using System.Security.Cryptography;
...
private static string MD5(string text)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] hashValue;
    byte[] message = UE.GetBytes(text);

    MD5 hashString = new MD5CryptoServiceProvider();
    string hex = "";

    hashValue = hashString.ComputeHash(message);
    foreach (byte x in hashValue)
    {
        hex += String.Format("{0:x2}", x);
    }
    return hex;
}

di GuiZ
08/07/2015

Commenta

We'll never share your email with anyone else.