views:

1148

answers:

6

I am trying to generate equivalent MD5 hashes in both JavaScript and .Net. Not having done either, I decided to use against a third party calculation - this web site for the word "password". I will add in salts later, but at the moment, I can't get the .net version to match up with the web site's hash:

5f4dcc3b5aa765d61d8327deb882cf99

I'm guessing it is an encoding problem, but I've tried about 8 different variations of methods for calculating an MD5 hash in .Net, and none of them match what I have obtained in JavaScript (or from the web site). This MSDN example is one of the methods I have tried, which results in this hash which i have commonly received:

7c6a180b36896a0a8c02787eeafb0e4c

Edit: Sadly, I've accidentally been providing different source strings to the two different implementations. EBSAK. :-/ Still be interested to hear your answer to the follow-up.

Bonus question: what encoding/format would be best to store hashed values in a database?

A: 

I get the same value as that web site for the word "password":

$ echo -n password | md5
5f4dcc3b5aa765d61d8327deb882cf99

Without seeing the code you are actually using, it's hard to tell what might be going wrong.

As for storing hashes in a database, I store them as a hex string. Although most databases can handle binary blobs, storing them as binary only saves half the space and they're harder to query and manipulate. Chances are the other data you're storing along with the hash is larger anyway.

Greg Hewgill
Or use Base64, which only expands the binary by a third instead of doubling it. Not that MD5 is safe to use for password hashes.
Steven Sudit
A: 

This VB.Net version gives the same results as MySQL from my own experience:

Private Function MD5Hash(ByVal str As String) As String

    Dim md5 As MD5 = MD5CryptoServiceProvider.Create
    Dim hashed As Byte() = md5.ComputeHash(Encoding.Default.GetBytes(str))
    Dim sb As New StringBuilder

    For i As Integer = 0 To hashed.Length - 1
        sb.AppendFormat("{0:x2}", hashed(i))
    Next

    Return sb.ToString

End Function
Mark Glorie
A: 

Do you have any code how you are trying to do this?

(response to second Q)I typically use a string field and store it as BASE64 encoding. Easy enough to work with and make comparisons.

/// <summary>
/// Gets the Base 64 encoded SHA1 hashed password
/// </summary>
/// <returns>A Base 64 encoded string representing the SHA1 Hash of the password</returns>
public string ToBase64SHA1String()
{
    return Convert.ToBase64String(this.GetSHA1HashCode());

}
mattlant
I had linked to one of the methods I was using - in the post.
pc1oad1etter
+2  A: 

Running the code from the MSDN site you quote:

 // Hash an input string and return the hash as
    // a 32 character hexadecimal string.
    static string getMd5Hash(string input)
    {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }


        static void Main(string[] args)
        {
            System.Console.WriteLine(getMd5Hash("password"));
        }

returns:

5f4dcc3b5aa765d61d8327deb882cf99
shoosh
True. :-/ I was using different source strings in the two implementations. Idiot!
pc1oad1etter
+2  A: 

To step back from this discussion a bit, I feel that Thomas Ptacek's post (in response to a Jeff Atwood post on a similar topic) explains best why you should not use anything like MD5 for password hashing. Recommended reading.

Chris Jester-Young
A: 

It should also be noted that MD5 sums can be cracked with rainbow tables (there are free programs on the internet that will accept an MD5 sum as an input and will output a plaintext - which is normally a password)

SHA1 is probably a better choice...

EDIT: adding salt is a good way to prevent being your hash from being reversed
EDIT 2: if I had bothered to read your post I would've noticed you already mentioned that you plan to add salt

advs89