Never for passwords
MD5 must not be used to store passwords. It is extremely fast, so leaked MD5 password hashes are cracked by brute force and precomputed tables in bulk. Use PBKDF2 (ASP.NET Core Identity's choice) or bcrypt. MD5 is also broken for collisions: two different inputs with the same MD5 can be made on purpose, so it cannot prove a file was not tampered with.
Where MD5 is still fine
Where nobody is attacking it: detecting accidental corruption, cache keys, deduplication, and matching systems that already use it (some APIs and storage services report MD5 checksums, such as the Content-MD5 header). For anything security-related, use SHA-256.
Why is my C# hash different?
The algorithm is the same everywhere; the bytes going in are not. The usual reasons, in order:
- Encoding.Unicode instead of Encoding.UTF8.
Encoding.Unicodeis UTF-16: two bytes per character, so every hash is different from what online tools and other languages give for the same text. UseEncoding.UTF8. Switch "The text is" above to UTF-16 to see the difference. - A line break.
echo "text" | sha256sumhashes "text\n". Useecho -norprintf. Text read from a file may also end with a line break, or start with a UTF-8 byte order mark. - Upper or lower case hex.
Convert.ToHexStringwrites uppercase,Convert.ToHexStringLower(.NET 9 and later) lowercase, andBitConverter.ToStringadds dashes. Compare hashes as bytes, or both in the same case.
FAQ
Is MD5 secure?
No. It is broken for collisions and far too fast for passwords. Use it only for non-security checksums and compatibility.
How long is an MD5 hash?
16 bytes: 32 hexadecimal characters.
How do I compute MD5 in C#?
MD5.HashData(Encoding.UTF8.GetBytes(text)), then Convert.ToHexStringLower (.NET 9 and later) or Convert.ToHexString(hash).ToLowerInvariant().
Can MD5 be decrypted?
No, it is a hash, not encryption. Sites that "decrypt" MD5 look the hash up in tables of hashes of common inputs.