Main Page   Reference Manual   Compound List   File List  

libecc::sha1 Class Reference

SHA-1. More...

#include <sha.h>

List of all members.

Public Member Functions

 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 Member Functions

void reset ()
 Reset the state of the SHA-1 object.
void process_block (bitset_digit_t const *block)
 Process the next data block.

Detailed Description

SHA-1.

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 than 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 & Destructor Documentation

libecc::sha1::sha1 ( void   ) 

Constructor. Constructs a SHA-1 object.


Member Function Documentation

bitset< 160 > libecc::sha1::digest ( void   )  const

The resulting hash value.

This method returns the hash value of the last message that was processed, as a 160 bit bitset.

void libecc::sha1::process_block ( bitset_digit_t const *  block  )  [protected]

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 bitset_digit_t having a total size of 512 bits.

Referenced by process_msg().

template<unsigned int n>
void libecc::sha1::process_msg ( bitset< n > const &  message,
size_t  number_of_bits = n 
) [inline]

Process a complete message.

Parameters:
message A bitset containing the message that the hash has to be calculated for.  Only the first number_of_bits least significant bits are considered to be part of the message.
number_of_bits The number of bits in the message.  This value must be less or equal to the size of the bitset.

References process_msg().

Referenced by process_msg().

void libecc::sha1::process_msg ( bitset_digit_t const *  message,
size_t  number_of_bits 
)

Process a complete message.

Calculate the hash value of a message of number_of_bits, passed as an array of bitset_digit_t.  The last bit of the message is the least significant bit of the first digit.

Parameters:
message A pointer to an array of bitset_digit_t, containing the message bits.
number_of_bits The number of valid bits in the array, only the first number_of_bits are considered to be part of the message.

Example:

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';
 }

References process_block(), libecc::bitset_base< N >::reset(), reset(), and libecc::bitset_base< N >::set().

void libecc::sha1::reset ( void   )  [protected]

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

Referenced by process_msg().

Copyright © 2002-2008 Carlo Wood.  All rights reserved.