1

Just for an example of creating a MD5 hash, microsoft provided this code sample:

MSDN Code

// in Helpers class
public static string GetMd5Hash(MD5 md5Hash, string input)
{
    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
    StringBuilder sBuilder = new StringBuilder(); 
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }
    return sBuilder.ToString();
}

and using it would be done like this

public string CreateHash(string str)
{
    using (var md5Hash = MD5.Create())
    {
        return Helpers.GetMd5Hash(md5Hash, str);
    }
}

My Code

I thought changing the code Microsoft provided to this, would make it easier regarding reusability of the helper method:

// in Helpers class
public static string GetMd5Hash(string input)
{
    using (var md5Hash = MD5.Create())
    {
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder(); 
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }
}

So reusing it creates less code lines as a result:

public string CreateHash(string str)
{
    return Helpers.GetMd5Hash(str);
}

So what is the purpose of not doing it like I did in my second example? My initial thought was, that programmers, who use the code MSDN provided, would always be aware of the fact, that they use unmanaged resources (is this the right term?), whereas they wouldn't if they used my implementation.

Any other thoughts?

Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
  • _Encrypt_ and _Hash_ are opposites. – SLaks Mar 03 '13 at 14:57
  • did you just move the `using` statement from `CreateHash` to `GetMd5Hash` I don't see anything different in the code other than that? If thats the case then I don't see how its simpler? – Zaid Amir Mar 03 '13 at 14:59
  • @SLaks: I updated my post. I'm not an expert in this, so I don't know the terminoligy. This was just the first code in reach for creating this example. – Michael Schnerring Mar 03 '13 at 14:59
  • @RedSerpent: Exaclty. I thought there is a reason, why Microsoft didn't chose to put the using into the body of `GetMd5Hash`. – Michael Schnerring Mar 03 '13 at 15:00
  • And you really think there is any difference in these sets of codes? – ssilas777 Mar 03 '13 at 15:01
  • I guess the MSDN code would be more efficient if you had to Encrypt multiple strings - it would only be created/disposed once and in your example it would be disposed of for each string? – Pleun Mar 03 '13 at 15:01
  • @ssilas777: I do not know anything about *unmanaged resources*, so I thought there might be a difference. – Michael Schnerring Mar 03 '13 at 15:01
  • 1
    Im not sure your explanation is correct, as anyone who will be using `Encrypy` is still going to be unaware of the underlying md5 object. There are different overloads of `Create`, so this might be a reason for creating the `MD5` outside the `Encrypt` method. also msdn is usually far from being perfect with its samples. Also, @SLaks, Encrypt and Hash are not opposites. – YavgenyP Mar 03 '13 at 15:04

2 Answers2

2

The MSDN code would be more efficient if you wanted to reuse the md5Hash in the scope of CreateHash

Zaid Amir
  • 4,727
  • 6
  • 52
  • 101
2

This sample from msdn is abit more generic, as there are different overloads of Create on the MD5 class, allowing to use different hashing algorithms.

Notice how they use it: whoever uses this function can provide any instance of the MD5 class, and it will work. Of course, as I said in my comment, their samples are usually far from being perfect and I dont think too much though's been put in it

YavgenyP
  • 2,113
  • 14
  • 11