How much does MD5 generation in .NET cost?

To interact with an external data feeder, I need to transfer a rolling security key that has been MD5 hashed (every day we need to create a new hashed MD5 key).

I trade whether it needs to be done every time we call an external channel or not. I need a string to contain about 10 characters.

This is for an ASP.NET site (C # /. NET 3.5), and the feed is used on almost every page. Can I best generate a hash once a day and then store it in the application cache, and also take a memory hit or generate it for every request?

+7
performance c # md5
source share
5 answers

The only acceptable basis for optimization is data. Measure the creation of this inline and measure its caching.

My high-end workstation can compute over 100K MD5 hashes of a 10-byte data segment per second. It would be zero gain from caching this for me, and I bet it is the same for you.

+14
source share

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

+3
source share

If it is the same thing for caching during the day, this might be an idea. You can even set the cache in 24 hours and write code to recover the hash when the cache expires

0
source share

Calculate the time complexity of the algorithm!

Take a 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 calculated the time complexity, we would get T = 11 + n * 2, but it’s just “what we see”, that is, ToLower can do the hard work that we don’t know. But from now on, we see that this algorithm is O (n) in all cases. The value of time grows as data grows.

Also, in order to address the cache problem, I would prefer to have “hard” work in memory, since memory is cheaper compared to using a CPU.

0
source share

Using the Asp.Net cache is very simple, so I don’t understand why you should not cache the key.

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

0
source share

All Articles