# Back to Basics – Calculating MD5 Hashing

Yesterday I needed to
use a hashing algorithm
I chose to use the MD5
hashing algorithm and
in this post I’ll show how
to calculate MD5 hash from a given string.

### What is MD5 Hashing Algorithm?

MD5 is a very widely used hashing function that commonly used to
check the integrity of files or strings. When using MD5 There is a
small possibility of getting two identical hashes to two different strings.
It is very useful for storing passwords in databases.

### Calculating MD5 Hash from a String

MD5 hash function is provided in the BCL of .NET inside
System.Security.Cryptography namespace.
The following code shows how to use the MD5 function to hash a given
string:

`public string CalculateMD5Hash(string input)`
`{`
`    var md5Function = MD5.Create();`
`    var inputBytes = Encoding.ASCII.GetBytes(input);`
`    var hash = md5Function.ComputeHash(inputBytes);`
` `
`    var sb = new StringBuilder();`
`    for (int i = 0; i < hash.Length; i++)`
`    {`
`        sb.Append(hash[i].ToString("X2"));`
`    }`
`    return sb.ToString();`
`}`

The code will creates MD5 hash algorithm class which is used to compute

the hash for the input string. After I’m computing the hash, I convert the
128 bit value to Hexadecimal upper case representation. If you want to use

lower case just replace the appending of the StringBuilder to the following

line of code:

`sb.Append(hash[i].ToString("x2"));`

After running this method on a given "Some String" string the result will be:

83BEB8C4FA4596C8F7B565D390F494E2

### Summary

Computing hash using the MD5 hash algorithm class is very simple and straight
forward. In the post I showed how to do that using the BCL MD5 class.