digestpp  0.01
Experimental C++11 header-only message digest library.
Public Member Functions | List of all members
digestpp::hasher< HashProvider, Mixin > Class Template Reference

Main class template implementing the public API for hashing. More...

#include <hasher.hpp>

Inheritance diagram for digestpp::hasher< HashProvider, Mixin >:

Public Member Functions

template<typename H = HashProvider, typename std::enable_if< std::is_default_constructible< H >::value >::type * = nullptr>
 hasher ()
 Default constructor. More...
 
template<typename H = HashProvider, typename std::enable_if<!detail::is_xof< H >::value >::type * = nullptr>
 hasher (size_t hashsize)
 Constructor with hash size parameter. More...
 
template<typename T , typename std::enable_if< detail::is_byte< T >::value >::type * = nullptr>
hasherabsorb (const T *data, size_t len)
 Absorbs bytes from a C-style pointer to character buffer. More...
 
template<typename T , typename std::enable_if< detail::is_byte< T >::value &&!std::is_same< T, std::string::value_type >::value >::type * = nullptr>
hasherabsorb (const std::basic_string< T > &str)
 Absorbs bytes from std::basic_string. More...
 
hasherabsorb (const std::string &str)
 Absorbs bytes from std::string. More...
 
template<typename T , typename std::enable_if< detail::is_byte< T >::value >::type * = nullptr>
hasherabsorb (std::basic_istream< T > &istr)
 Absorbs bytes from std::istream. More...
 
template<typename IT >
hasherabsorb (IT begin, IT end)
 Absorbs bytes from an iterator sequence. More...
 
template<typename T , typename H = HashProvider, typename std::enable_if< detail::is_byte< T >::value &&detail::is_xof< H >::value >::type * = nullptr>
void squeeze (T *buf, size_t len)
 Squeeze bytes into user-provided preallocated buffer. More...
 
template<typename OI , typename H = HashProvider, typename std::enable_if< detail::is_xof< H >::value >::type * = nullptr>
void squeeze (size_t len, OI it)
 Squeeze bytes into an output iterator. More...
 
template<typename H = HashProvider, typename std::enable_if< detail::is_xof< H >::value >::type * = nullptr>
std::string hexsqueeze (size_t len)
 Squeeze bytes and return them as a hex string. More...
 
template<typename T , typename H = HashProvider, typename std::enable_if< detail::is_byte< T >::value &&!detail::is_xof< H >::value >::type * = nullptr>
void digest (T *buf, size_t len) const
 Output binary digest into user-provided preallocated buffer. More...
 
template<typename OI , typename H = HashProvider, typename std::enable_if<!detail::is_xof< H >::value >::type * = nullptr>
void digest (OI it) const
 Write binary digest into an output iterator. More...
 
template<typename H = HashProvider, typename std::enable_if<!detail::is_xof< H >::value >::type * = nullptr>
std::string hexdigest () const
 Return hex digest of absorbed data. More...
 
void reset (bool resetParameters=false)
 Reset the hasher state to start new digest computation. More...
 

Detailed Description

template<class HashProvider, template< class > class Mixin = mixin::null_mixin>
class digestpp::hasher< HashProvider, Mixin >

Main class template implementing the public API for hashing.

Individual hash functions are defined by typedefs. See digestpp namespace description for description of supported hash functions with usage examples.

Parameters
HashProviderA class implementing the algorithm via traditional init/update/final interface.
MixinA class template which can be used to inject additional functions to the public API of the hasher.
See also
digestpp

Constructor & Destructor Documentation

◆ hasher() [1/2]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename H = HashProvider, typename std::enable_if< std::is_default_constructible< H >::value >::type * = nullptr>
digestpp::hasher< HashProvider, Mixin >::hasher ( )
inline

Default constructor.

Available if
  • HashProvider is a hash function with fixed output size, OR
  • HashProvider is a hash function with sensible default output size, OR
  • HashProvider is an extendable output function (XOF)

◆ hasher() [2/2]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename H = HashProvider, typename std::enable_if<!detail::is_xof< H >::value >::type * = nullptr>
digestpp::hasher< HashProvider, Mixin >::hasher ( size_t  hashsize)
inline

Constructor with hash size parameter.

Supported output sizes for each algorithm are listed in the description of corresponding typedef.

Available if
  • HashProvider supports multiple output sizes, AND
  • HashProvider is not an extendable output function (XOF)
Parameters
[in]hashsizeDesired output digest size (in bits).
Exceptions
std::runtime_errorif the requested output size is not supported by the algorithm.

Member Function Documentation

◆ absorb() [1/5]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename T , typename std::enable_if< detail::is_byte< T >::value >::type * = nullptr>
hasher& digestpp::hasher< HashProvider, Mixin >::absorb ( const T *  data,
size_t  len 
)
inline

Absorbs bytes from a C-style pointer to character buffer.

Parameters
[in]dataPointer to data to absorb
[in]lenSize of data to absorb (in bytes)
Returns
Reference to *this
Example:
// Calculate SHA-512/256 digest of a C array and output it in hex format
unsigned char c[32];
std::iota(c, c + sizeof(c), 0);
cout << digestpp::sha512(256).absorb(c, sizeof(c)).hexdigest() << std::endl;

◆ absorb() [2/5]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename T , typename std::enable_if< detail::is_byte< T >::value &&!std::is_same< T, std::string::value_type >::value >::type * = nullptr>
hasher& digestpp::hasher< HashProvider, Mixin >::absorb ( const std::basic_string< T > &  str)
inline

Absorbs bytes from std::basic_string.

Parameters
[in]strString to absorb
Returns
Reference to *this

◆ absorb() [3/5]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
hasher& digestpp::hasher< HashProvider, Mixin >::absorb ( const std::string &  str)
inline

Absorbs bytes from std::string.

Parameters
[in]strString to absorb
Returns
Reference to *this
Example:
// Calculate BLAKE2b-256 digest from an std::string and output it in hex format
std::string str = "The quick brown fox jumps over the lazy dog";
std::cout << digestpp::blake2b(256).absorb(str).hexdigest() << std::endl;

◆ absorb() [4/5]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename T , typename std::enable_if< detail::is_byte< T >::value >::type * = nullptr>
hasher& digestpp::hasher< HashProvider, Mixin >::absorb ( std::basic_istream< T > &  istr)
inline

Absorbs bytes from std::istream.

Parameters
[in]istrStream to absorb
Returns
Reference to *this
Example:
// Calculate SHA-256 digest of a file and output it in hex format
std::ifstream file("filename", std::ios_base::in|std::ios_base::binary);
std::cout << digestpp::sha256().absorb(file).hexdigest() << std::endl;

◆ absorb() [5/5]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename IT >
hasher& digestpp::hasher< HashProvider, Mixin >::absorb ( IT  begin,
IT  end 
)
inline

Absorbs bytes from an iterator sequence.

Parameters
[in]beginBegin iterator
[in]endEnd iterator
Returns
Reference to *this
Example:
// Calculate SHA-512 digest of a vector and output it in hex format
std::vector<unsigned char> v(100);
std::iota(v.begin(), v.end(), 0);
std::cout << digestpp::sha512().absorb(v.begin(), v.end()).hexdigest() << std::endl;

◆ digest() [1/2]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename T , typename H = HashProvider, typename std::enable_if< detail::is_byte< T >::value &&!detail::is_xof< H >::value >::type * = nullptr>
void digestpp::hasher< HashProvider, Mixin >::digest ( T *  buf,
size_t  len 
) const
inline

Output binary digest into user-provided preallocated buffer.

This function does not change the state of the hasher and can be called multiple times, producing the same result. To reset the state and start new digest calculation, use reset function.

Available if
HashProvider is a hash function (not XOF)
Parameters
[out]bufBuffer to squeeze data to; must be of byte type (char, unsigned char or signed char)
[in]lenSize of the buffer
Exceptions
std::runtime_errorif the buffer size is not enough to fit the calculated digest (fixed by the algorithm or specified in the hasher constructor).
Example:
// Output binary digest to a raw C array
unsigned char buf[32];
digestpp::sha3(256).absorb("The quick brown fox jumps over the lazy dog").digest(buf, sizeof(buf));

◆ digest() [2/2]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename OI , typename H = HashProvider, typename std::enable_if<!detail::is_xof< H >::value >::type * = nullptr>
void digestpp::hasher< HashProvider, Mixin >::digest ( OI  it) const
inline

Write binary digest into an output iterator.

This function does not change the state of the hasher and can be called multiple times, producing the same result. To reset the state and start new digest calculation, use reset function.

Available if
HashProvider is a hash function (not XOF)
Parameters
[out]itOutput iterator to a byte container.
Example:
// Output binary SHA3-256 digest to a vector
vector<unsigned char> v;
digestpp::sha3(256).absorb("The quick brown fox jumps over the lazy dog").digest(back_inserter(v));

◆ hexdigest()

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename H = HashProvider, typename std::enable_if<!detail::is_xof< H >::value >::type * = nullptr>
std::string digestpp::hasher< HashProvider, Mixin >::hexdigest ( ) const
inline

Return hex digest of absorbed data.

This function does not change the state of the hasher and can be called multiple times, producing the same result. To reset the state and start new digest calculation, use reset function.

Available if
HashProvider is a hash function (not XOF)
Returns
Calculated digest as a hexademical string
Example:
// Calculate BLAKE2b digest from a double quoted string and output it in hex format
std::cout << digestpp::blake2b().absorb("The quick brown fox jumps over the lazy dog").hexdigest() << std::endl;

◆ hexsqueeze()

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename H = HashProvider, typename std::enable_if< detail::is_xof< H >::value >::type * = nullptr>
std::string digestpp::hasher< HashProvider, Mixin >::hexsqueeze ( size_t  len)
inline

Squeeze bytes and return them as a hex string.

After each invocation of this function the internal state of the hasher changes so that the next call will generate different (additional) output bytes. To reset the state and start new digest calculation, use reset function.

Available if
HashProvider is an extendable output function (XOF)
Parameters
[in]lenSize of data to squeeze (in bytes)
Returns
Calculated digest as a hexademical string
Example:
// Generate 64-byte digest using customizable cSHAKE-256 algorithm and print it in hex format
xof.set_customization("My Customization");
std::cout << xof.absorb("The quick brown fox jumps over the lazy dog").hexsqueeze(64) << std::endl;

◆ reset()

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
void digestpp::hasher< HashProvider, Mixin >::reset ( bool  resetParameters = false)
inline

Reset the hasher state to start new digest computation.

Parameters
[in]resetParametersif true, also clear optional parameters (personalization, salt, etc)

◆ squeeze() [1/2]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename T , typename H = HashProvider, typename std::enable_if< detail::is_byte< T >::value &&detail::is_xof< H >::value >::type * = nullptr>
void digestpp::hasher< HashProvider, Mixin >::squeeze ( T *  buf,
size_t  len 
)
inline

Squeeze bytes into user-provided preallocated buffer.

After each invocation of this function the internal state of the hasher changes so that the next call will generate different (additional) output bytes. To reset the state and start new digest calculation, use reset function.

Available if
HashProvider is an extendable output function (XOF)
Parameters
[out]bufBuffer to squeeze data to; must be of byte type (char, unsigned char or signed char)
[in]lenSize of data to squeeze (in bytes)

◆ squeeze() [2/2]

template<class HashProvider , template< class > class Mixin = mixin::null_mixin>
template<typename OI , typename H = HashProvider, typename std::enable_if< detail::is_xof< H >::value >::type * = nullptr>
void digestpp::hasher< HashProvider, Mixin >::squeeze ( size_t  len,
OI  it 
)
inline

Squeeze bytes into an output iterator.

After each invocation of this function the internal state of the hasher changes so that the next call will generate different (additional) output bytes. To reset the state and start new digest calculation, use reset function.

Available if
HashProvider is an extendable output function (XOF)
Parameters
[in]lenSize of data to squeeze (in bytes)
[out]itoutput iterator to a byte container
Example:
// Generate long output using SHAKE-256 extendable output function using multiple calls to squeeze()
std::vector<unsigned char> v;
xof.absorb("The quick brown fox jumps over the lazy dog");
xof.squeeze(1000, back_inserter(v));
xof.squeeze(1000, back_inserter(v));
xof.squeeze(1000, back_inserter(v));
std::cout << "Squeezed " << v.size() << " bytes." << std::endl;

The documentation for this class was generated from the following file: