Iterative / Additive MD5

I need to create a checksum over a dictionary. Keys and values.

Is there an easy way to do this in an iterative way.

foreach (var element in dic.Keys) checksum + = checksum (dic [item]) + checksum (item);

In this case, the keys and values ​​can be converted to strings, concatenated, and then one checksum applied to them, but is there a better way?

Ideally MD5, but other options might work. Using this to validate data that is being transferred over multiple storage methods. The checksum is then encrypted along with some other information (using AES), so I'm not terribly concerned about a perfect, unbreakable checksum.

+2


a source to share


3 answers


Answered my own question I guess ....



GetHashCode () for each item. Add them to the untested environment {}. Too easy.

0


a source


Generating a signature is much the same process: create an MD5 hash object, then you digest all the bytes of interest, then you extract the hash value. The important thing is that you and the verifier match the bytes with the hash and in the order in which they are hashed.

In C #, you can achieve this by calling HashAlgorithm.TransformBlock multiple times and then finally calling HashAlgorithm.TransformFinalBlock . This is automated with CryptoStream using HashTransform ( MD5 implements ICryptoTransform) and then just writes the dictionary to the cryptostream.



As a side note, the countless protocols and cryptograms that digest the hash and encrypt it have been humiliated in the wild. I would suggest taking the beaten path and using well established industry standards:

  • Use HMAC, see HMACMD5
  • Use RSA signature (for example encryption with MD5 hash private key) and save your self from all key provisioning and secret exchange issues, see RSACryptoServiceProvider.SignHash
+4


a source


You shouldn't write new code that relies on MD5. It is outdated and for some very good reason. You should look at SHA-256 or at least SHA-1 instead

And you must take Remus's advice. Cryptography + hashes = digital signatures. Pull something off the shelf (just not XML-Security, please!), Explore it, use it, and move on to other interesting parts of your project.

0


a source







All Articles