MD5

Author: Matthew Wegner

Overview
This C# code snippet generates an MD5 hash for an input string. The formatting will match the output of PHP's md5 function.

C#
Best placed in your static-only utility class.

JavaScript
... and just in case anyone was wondering. This is also possible using JavaScript.

To use, name your Javascript file something like "md5functions.js". In your code, access the function using "hash = md5functions.Md5Sum("string");", where the prefix of the function matches the name of the .js file you created for it.

You can use SHA1CryptoServiceProvider instead of MD5CryptoServiceProvider if you want to create SHA1 hashes instead of MD5 hashes.

The server side
As noted above, the above unity snippets will return a hash matching the one returned from PHP's md5 function. In case you are using another language on the server side, here are some examples:

Perl
use Digest::MD5 qw(md5_hex);

my $hashString = md5_hex($stringToHash);

PHP
$hashString = md5($stringToHash);

Python
import hashlib

def md5Sum(inputString): return hashlib.md5(inputString).hexdigest

Ruby
require 'digest/md5'

def md5Sum(inputString) Digest::MD5.hexdigest(inputString) end

Shell
Requires that you have the md5sum program installed on the server. HASH = `echo "$STRING_TO_HASH" | md5sum | cut -f 1 -d' '`