This algorithm implementation is a part of my cryptography implementations project. The full project, along with licensing information and more detail, is hosted on GitHub.
This is an implementation of the SHA-1 hash algorithm.
A MD5 structure that will hold all hash-related data and calculations as the hash is calculated.
Initializes the SHA1_CTX object.
SHA1_update(SHA1_CTX *ctx, unsigned char data, int len)
Once an object has been created and initialized, the data to be hashed must be added. Due to practical limitations, it may not be optimal (or possible) to add all the data to the SHA1 hash in one data chunk, so the function inputs, stores, and calculates data as it is received, allowing the data to be added in as many chunks as necessary.
unsigned char data-- This is the data to be added to the hash.
int len-- This is the length, in bytes, of the data in the "data" array.
SHA1_final(SHA1_CTX *ctx, unsigned char hash)
Finalize and output the hash.
unsigned char hash-- This is the array to store the output hash. It must be at least 16 bytes in size.
- Create an SHA1_CTX object.
- Initialize it with sha1_init().
- Read some/all of the data to hash into an array, calculate the size of the data, and add it to the hash with sha1_update().
- Repeat the previous step for all the data you want to hash.
- Finalize and output the hash with sha1_final().
Repeat steps (2) to (5) for as many hashes as you want to calculate.
The 32-bit words (which in this case are unsigned integers) used in the code use little endian byte ordering. The SHA-1 specification uses the big endian byte order, so some byte-reversals are made when copying data into and out of integers in this code.
This algorithm has not been well optimized, although some general attempts have been made to that effect. This algorithm has passed numerous test vectors (including all official ones) successfully.
This algorithm can hash data up to 264 bits (2,147,483,648 gigabytes) in length.
It is worth noting that SHA-1 is no longer considered a perfectly secure hash algorithm. It is recommended that a hash algorithm such as Whirlpool or RIPE-MD, or even SHA-1 with double the number of standard rounds, be used in when security is critical.