How to

create MD5 from string

Published: 14. December 2008 | Updated: 14. December 2008
License: Microsoft Public License (MS-PL)
Categories: Security
Tags: C# Security Text
Was this snippet helpful for you? YESYES / NONO

Snippet shows how to generate MD5 hash from string

Import namespaces

using System.Security.Cryptography;
using System.Text;

Code

string text = "abc";

MD5 md5 = MD5.Create();
byte[] bytes = Encoding.UTF8.GetBytes(text);

StringBuilder sb = new StringBuilder();

foreach (byte b in md5.ComputeHash(bytes))
{
    sb.Append(b.ToString("X2"));
}

Console.WriteLine(sb.ToString());
Console Output:
900150983CD24FB0D6963F7D28E17F72
Send us feedback about this snippet »



Related Snippets: