SHA-1 hashing algorithm


A small class implementing the SHA1 hashing algorithm.

SHA (Secure Hash Algorithm) is a cryptographically safe function developed by NIST and NSA in 1992. It fulfills all the requirements imposed for one-way hashing functions and it generates very different images for similar sequences. This code is based on the 100% free public domain implementation by Dominik Reichl.

NO WARRANTY is given for this code. Use it at your own risk. It should always compute correct SHA1 digests, but no security-related tests have been made.


  • 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.
  • Endianness-aware (see the SHA1_LITTLE_ENDIAN #define in the header file).

Usage example

Compute SHA1 hash for a file or byte array:

#include <QByteArray> #include "sha1hash.h" // ... // You better chose a different filename ;) QString filename("/dev/random"); QString s("Hello World!"); QByteArray buffer = s.toAscii(); QString digest1 = SHA1Hash::hashFile(filename); // Let's hash the world!! QString digest2 = SHA1Hash::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" used variables before leaving the hashFile() method, so simply remove the comments at the end of the method.