Skip to main content
Email Password   helpLost your password?

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 of 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 whether 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!

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralOutput bug Pin
vcor
12:26 5 Mar '09  
GeneralRe: Output bug Pin
Dominik Reichl
23:55 17 Mar '09  
Generalsmall patch to use short hex output values Pin
thomasdev
1:32 19 Feb '09  
GeneralRe: small patch to use short hex output values Pin
Dominik Reichl
23:56 17 Mar '09  
GeneralRe: small patch to use short hex output values Pin
thomasdev
0:51 18 Mar '09  
GeneralHMAC-SHA receiver processing? Pin
Adam Harding
13:52 3 Jun '08  
GeneralRe: HMAC-SHA receiver processing? Pin
Dominik Reichl
6:11 4 Jun '08  
GeneralUseful! Pin
Dlt75
4:00 15 Nov '07  
GeneralUndefined reference to CSHA1::Update Pin
RussellGilbert
12:37 15 Jul '07  
AnswerRe: Undefined reference to CSHA1::Update Pin
bruno1999
10:37 5 Mar '08  
QuestionUTF16 encoding? Pin
TuringMachine
11:38 31 May '07  
AnswerRe: UTF16 encoding? Pin
Dominik Reichl
23:10 31 May '07  
GeneralThere is a overrun bug Pin
missilry
14:58 4 Dec '06  
GeneralRe: There is a overrun bug Pin
Dominik Reichl
23:52 4 Dec '06  
QuestionSHA1 Hash does not give same hash as NET SHA1 Pin
Kilty
2:34 19 Nov '06  
AnswerRe: SHA1 Hash does not give same hash as NET SHA1 Pin
Dominik Reichl
8:53 30 Nov '06  
GeneralRe: SHA1 Hash does not give same hash as NET SHA1 Pin
Kilty
9:13 30 Nov '06  
QuestionLimitation: Adding iostream Gives Compiler Errors Pin
alam00
9:53 20 May '06  
AnswerRe: Limitation: Adding iostream Gives Compiler Errors Pin
Kapali Viswanathan
8:05 6 Jun '06  
Generalerreur LIBCD.lib Pin
ramzi2006
23:54 18 Feb '06  
Generalconst char * Pin
bramp
5:30 10 Jan '06  
GeneralThanks for contributing this Pin
GadgetMan66
4:46 21 Dec '05  
Generalthanx a lot! Pin
zoz
3:09 27 Nov '05  
Generaladding iostream and fstream Pin
nmullane
18:20 22 Oct '05  
GeneralRe: adding iostream and fstream Pin
Miroslav Rajcic
2:57 16 Oct '06  


Last Updated 17 Mar 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009