MD5 hashing algorithm


A small class implementing Ron Rivest's MD5 hashing algorithm.

The class is based on the code written by Colin Plumb in 1993 (my understanding is that no copyright is claimed and that this code is in the public domain).


  • 2006-06-09 Minor changes (documentation and binary compatibility).
  • 2005-10-02 Added some methods to hash byte arrays.
  • 2005-09-30 First public release.


  • Qt 4.0.x (QtCore module)


  • Allows to compute the hash of a single byte array and of a whole file.

Known issues and bugs

  • There might be problems with Big Endian byte order (as on SPARC and Motorola CPUs)

Usage example

Compute MD5 hash for a file or byte array:

#include <QByteArray> #include "md5hash.h" // ... // You better chose a different filename ;) QString filename("/dev/random"); QString s("Hello World!"); QByteArray buffer = s.toAscii(); QString digest1 = MD5Hash::hashFile(filename); // Let's hash the world!! QString digest2 = MD5Hash::hashData(buffer); // ...or just some bytes // ...

Everything is static, so you don't need to instantiate any class.

Technical details

Sensitive data

If you are handling sensitive data you may want to "clean" the md5Context struct before leaving the hashFile() method. You can use a simple memset(context, 0, sizeof(context)) for this purpose.


Colin Plumb's original implementation is indianness-aware. I removed all the calls to a function called byteReverse() as I didn't need Big Indian CPU support (sorry Motorola-CPU freaks ;).