An MD5 hash turns any input into a 128-bit value, usually written as 32 hexadecimal characters. The same input always produces the same hash, so MD5 is handy for cache keys, detecting changed files or matching records against a legacy system that already stores MD5 values.
Since .NET 5, the whole job takes one line.
Hash a string
using System.Security.Cryptography;
using System.Text;
string hash = CreateMd5Hash("http://www.csharpexamples.com");
Console.WriteLine(hash);
// E426A83F137FA324C3412B92193EED1F
static string CreateMd5Hash(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
byte[] hash = MD5.HashData(bytes);
return Convert.ToHexString(hash);
}
MD5.HashData is a static method, so there is no object to create or dispose. Convert.ToHexString returns uppercase characters. If the system you compare against uses lowercase, call Convert.ToHexStringLower on .NET 9 and later, or .ToLowerInvariant() on older versions.
Pick the encoding on purpose
A hash is computed from bytes, not from text, so the same string can produce different hashes depending on how you encode it. Older examples often use Encoding.ASCII. ASCII silently replaces every character outside its 128-character range with ?, which means "café" and "caf?" get the same hash. Use Encoding.UTF8 unless you need to match a system that does something else.
Hash a file
For files, pass a stream instead of loading the whole file into memory. This overload exists from .NET 7:
await using FileStream stream = File.OpenRead("backup.zip");
byte[] hash = await MD5.HashDataAsync(stream);
Console.WriteLine(Convert.ToHexString(hash));
That is the same value md5sum on Linux or Get-FileHash -Algorithm MD5 in PowerShell reports, so you can check a download against the checksum published next to it.
When not to use MD5
MD5 has been broken for collision resistance since 2004. Anyone can create two different inputs with the same hash in seconds. That rules it out for:
- Storing passwords. Use a slow, salted algorithm instead. In .NET,
Rfc2898DeriveBytes.Pbkdf2(PBKDF2) is built in, and ASP.NET Core Identity’sPasswordHasherhandles the details for you. - Signatures or tamper detection, where an attacker could supply the input. Use SHA-256: replace
MD5.HashDatawithSHA256.HashDataand the rest of the code stays the same.
For checksums, cache keys and deduplication, where nobody is trying to forge a match, MD5 is still fine and fast.
On .NET Framework
MD5.HashData and Convert.ToHexString do not exist on .NET Framework 4.x. There, create an instance and build the hex string yourself:
static string CreateMd5Hash(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
return BitConverter.ToString(hash).Replace("-", "");
}
}
Good one
You should use UTF8 not ASCII for the input normalization as well as normalizing the input string for consistent hashing of strings with special/joint characters…
Encoding.UTF8.GetBytes(input.Normalize(NormalizationForm.FormKC))