Contents


Description of the Secure Hash Algorithm SHA-1

The Secure Hash Algorithm SHA-1 is a cryptographically secure one-way hash algorithm. It was designed by the NIST (National Institute of Standards and Technology), along with the NSA (National Security Agency). SHA-1 is based on the Message Digest MD4 algorithm design principles by Ronald L. Rivest of MIT.

Well, I think I don't have to explain what you can do with cryptographic hash algorithms. For an example what you can do with such algorithms, see this CodeProject article (CMD5 class).

For more information about SHA-1 see references [1] and [2].


CSHA1 class description

The CSHA1 class is an easy-to-use class for the SHA-1 hash algorithm.

If you want to test if your implementation of the class is working, try the test vectors in the 'TestVectors' directory in the demo zip file. You can find the correct final hash values in the header file of the CSHA1 class.

Class members of the CSHA1 class:


Hashing binary data and strings

CSHA1 sha1;
sha1.Update(string0, strlen(string0));
sha1.Update(string1, strlen(string1));
sha1.Update(binary2, uSizeOfBufferBinary2);
sha1.Update(binary3, uSizeOfBufferBinary3);
sha1.Final();

sha1.ReportHash(szReport, CSHA1::REPORT_HEX_SHORT);
// or
sha1.GetHash(binaryArray);

I will comment each line of the example above now.

First declare an instance of the CSHA1 class:

CSHA1 sha1;
Now hash in the data like this:
sha1.Update((unsigned char *)szString, strlen(szString));
You can call this method as often as you wish.

When you hashed in all data, call the Final() member function:

sha1.Final();
If you want to get the final message digest as a pre-formatted string use this:
sha1.ReportHash(szReport, CSHA1::REPORT_HEX_SHORT);
If you want to get the final message digest in "raw form":
sha1.GetHash(binaryArray); // Get the raw message digest bytes

Hashing files

Hashing files is the same process as hashing strings and binary data but instead of using the Update method you use the HashFile member function of the class.

For more comments see the string/binary data hashing example above.

CSHA1 sha1;
sha1.HashFile("TheFile.cpp"); // Hash in the contents of the file
                              // 'TheFile.cpp'
sha1.Final();

sha1.ReportHash(szReport, CSHA1::REPORT_HEX); // Get final hash as
                                              // pre-formatted string
// or
sha1.GetHash(binaryArray); // Get the raw message digest bytes to a
                           // temporary buffer

References

[1] RFC 3174: US Secure Hash Algorithm 1 (SHA1).
[2] Bruce Schneier, "Applied Cryptography", pages 442-445.


Version history


That's it! Happy hashing!





Copyright © 2003-2010 Dominik Reichl, [Imprint] [Disclaimer] [Acknowledgements]