Aaron Powell
Aaron Powell

Reputation: 25099

How expensive is MD5 generation in .NET?

To interact with an external data feed I need to pass a rolling security key which has been MD5 hashed (every day we need to generate a new MD5 hashed key).

I'm trading up whether or not to do it every time we call the external feed or not. I need to has a string of about 10 characters for the feed.

It's for an ASP.NET (C#/ .NET 3.5) site and the feed is used on pretty much every page. Would I best off generating the hash once a day and then storing it in the application cache, and taking the memory hit, or generating it on each request?

Upvotes: 8

Views: 7902

Answers (5)

Rune Grimstad
Rune Grimstad

Reputation: 36300

Using the Asp.Net cache is very easy so I don't see why you shouldn't cache the key.

Storing the key in cache may even save some memory since you can reuse it instead of creating a new one for each request.

Upvotes: 0

Filip Ekberg
Filip Ekberg

Reputation: 36287

Calculate the time complexity of the algorithm!

Look at the following code:

   public string GetMD5Hash(string input)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
        bs = x.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2").ToLower());
        }
        string password = s.ToString();
        return password;
    }

If we were to calculate the time complexity we would get T= 11 + n * 2 however this is just "what we see" i.e. ToLower might do some heavy work which we don't know. But from this point we can see that this algorithm is O(n) in all cases. Meaning time grows as data growns.

Also to adress the cache issue, I'd rather have my "heavy" work in Memory since memory is less expensive when compared to CPU-usage.

Upvotes: 1

Rad
Rad

Reputation: 8381

If it'll be the same for a given day caching it might be an idea. You could even set the cache to be 24 hours and write code to regenerate the hash when the cache expires

Upvotes: 0

Sander
Sander

Reputation: 26374

The only acceptable basis for optimizations is data. Measure generating this inline and measure caching it.

My high-end workstation can calculate well over 100k MD5 hashes of a 10-byte data segment in a second. There would be zero benefit from caching this for me and I bet it's the same for you.

Upvotes: 15

doppelfish
doppelfish

Reputation: 983

Generate some sample data. Well, a lot of it. Compute the MD5 of the sample data. Measure the time it takes. Decide for yourself.

Upvotes: 3

Related Questions