SHA-1 (2005-6-21)
This is a C++ class that implements the Secure Hash Algorithm SHA-1. The
purpose of the algorithm is to calculate a strong hash of given bit string.
By "strong", we mean that it is very difficult to find a different bit string
that results in the same hash. It is similarly difficult, using only the hash,
to determine the original bit string. SHA-1 produces a 20 byte (160 bit) hash.
Download
Class Usage
The class is called SHA1. The API consists of four methods:
SHA1::SHA1() is the class constructor
void SHA1::addBytes( const char* data, int num )
processes bytes into the hash.
- data is a pointer to the first byte to be added.
The bytes are not modified.
- num is the number of bytes to process.
unsigned char* SHA1::getDigest()
completes the hashing process and returns the final hash. The SHA1 instance
should not be used after calling this method.
- Returns a pointer to a 20-byte hash. Release the memory with free().
static void SHA1::hexPrinter( unsigned char* c, int l )
prints bytes to stdout in hexadecimal format.
- c is a pointer to the first byte to be printed.
The bytes are not modified.
- l is the number of bytes to print.
Example
The following program will print one line to stdout:
a9 99 3e 36 47 06 81 6a ba 3e 25 71 78 50 c2 6c 9c d0 d8 9d
#include <string.h>
#include <stdlib.h>
#include "sha1.h"
int main(int argc, char *argv[])
{
#define BYTES "abc"
SHA1* sha1 = new SHA1();
sha1->addBytes( BYTES, strlen( BYTES ) );
unsigned char* digest = sha1->getDigest();
sha1->hexPrinter( digest, 20 );
delete sha1;
free( digest );
}
License
I couldn't find a free SHA-1 implementation for C++ so I wrote my own.
I wrote the Python version first and then the C++ one. The source code
has an open source license (the so-called
"MIT License"
) which allows it to be used in commercial software.
Links
Secure Hash
Standard (FIP 180-1) is the official document from the U.S.
government which defines the algorithm.
Wikipedia has a very helpful
article on SHA.
|
Copyright © 1999-2007 Michael Leonhard
|
|
|