How do I calculate a MD5 hash from a string?

Avatar

It is a common practice to store passwords in databases using a hash. MD5 (defined in RFC 1321) is a common hash algorithm, and using it from C# is easy.


Here’s an implementation of a method that converts a string to an MD5 hash, which is a 32-character string of hexadecimal numbers.


An example call:


…returns a string like this:


To make the hex string use lower-case letters instead of upper-case, replace the single line inside the for loop with this line:


The difference is the ToString method parameter.


[author: Jani Järvinen, C# MVP]

0 comments

Leave a comment