Message Digest Hash (MD5) using .NET

MD5 is a widely-used cryptographic hash algorithm. It can be used to compute a 'hash' value or checksum for a piece of text.

Imagine that you want to send a document to someone, and you need to be sure that the document is not tampered with before it arrives. One way is to compute the MD5 hash of the document, which is highly sensitive to any changes in the contents. If anyone edits the document the MD5 hash will change.

You may wonder: If someone tampers with the document contents, can't they also just compute a new MD5 hash value? To prevent this, it's normal to encrypt the MD5 hash using a public/private key mechanism, or send the MD5 hash separately to the document.

Note that MD5 is not completely secure. It's possible in theory for two documents to have the same MD5 hash. Although this is extremely unlikely to happen by accident, it is difficult but possible to do deliberately.

The MD5 hash can be calculated using APL, as in this Dyalog example. However, it's also possible to use the Microsoft .NET framework, as in this APLX (no longer avilable) code:

      string←'The quick brown fox jumps over the lazy dog.'

      crypto← '.net' ⎕new 'System.Security.Cryptography.MD5CryptoServiceProvider'
      encoding←'.NET' ⎕new 'System.Text.ASCIIEncoding'

      hash←crypto.ComputeHash (encoding.GetBytes.⎕ref string)
      hash←,(⎕d,⎕a)[⎕io+⍉16 16⊤hash]

      hash
e4d909c290d0fb1ca068ffaddf22cbd0
      ⍴ hash
32




Author: SimonMarsden


CategoryDotNet

MessageDigestHashNet (last edited 2017-02-16 19:26:21 by KaiJaeger)