#include <sha.h>
Public Methods | |
sha1 (void) | |
Constructor. Constructs a SHA-1 object. | |
void | process_msg (bitset_digit_t const *message, size_t number_of_bits) |
Process a complete message. | |
template<unsigned int n> void | process_msg (bitset< n > const &message, size_t number_of_bits=n) |
Process a complete message. | |
bitset< 160 > | digest (void) const |
The resulting hash value. | |
Protected Methods | |
void | reset () |
Reset the state of the SHA-1 object. | |
void | process_block (bitset_digit_t const *block) |
Process the next data block. |
This class allows one to calculate a hash value of a bitset using the Secure Hash Algorithm 1, as described in the FIPS PUB 180-1.
Unlike most implementations, this is a bit oriented implementation and therefore more suitable to calculate hash values of bitsets then to calculate hash values of character strings (like for example a text file).
The correct use is as follows.
libecc::bitset<10> msg("3FF"); // A bit set of 10 ones. libecc::sha1 hash_obj; hash_obj.process_msg(msg); libecc::bitset<160> hash(hash_obj.digest());
|
Constructor. Constructs a SHA-1 object.
|
|
The resulting hash value.
This method returns the hash value of the last message that was processed, as a 160 bit bitset. |
|
Process the next data block.
This method will process the next block of data. The argument block should be a pointer to an array of |
|
Process a complete message.
|
|
Process a complete message.
Calculate the hash value of a message of number_of_bits, passed as an array of
The following code would correctly print the hash value of a string of characters.
void print_hash(std::string const& message) { using libecc::bitset_digit_t; size_t digits = (message.size() + sizeof(bitset_digit_t) - 1) / sizeof(bitset_digit_t); bitset_digit_t* buf = new bitset_digit_t[digits]; int shift = 0, digit = -1; for (std::string::const_reverse_iterator iter = message.rbegin(); iter != message.rend(); ++iter) { if (shift == 0) buf[++digit] = *iter; else buf[digit] |= *iter << 8 * shift; shift = (shift + 1) % sizeof(bitset_digit_t); } libecc::sha1 hash_obj; hash_obj.process_msg(buf, 8 * message.size()); delete [] buf; std::cout << hash_obj.digest() << '\n'; } |
|
Reset the state of the SHA-1 object.
This method needs to be called prior to calculating the hash value of a message with sha1::process_block. |