lhashes  1.0.0
C++ hashes library
limes::hash::Hasher Class Referenceabstract

A base class representing an object that calculates a hash function. More...

#include <lhashes_hash.h>

+ Inheritance diagram for limes::hash::Hasher:

Public Member Functions

virtual ~Hasher ()=default
 Destructor. More...
 
virtual std::string getHash ()=0
 Retrieves the calculated hash value as a string. More...
 
virtual std::size_t getLengthOfHash () const =0
 Returns the length of a hash string for this algorithm. More...
 
Appending data to be hashed
virtual void update (const unsigned char *input, std::size_t length)=0
 Updates the internal state of the hasher with some new data. More...
 
virtual void update (std::string_view input)
 Updates the internal state of the hasher with some new data. More...
 

Related Functions

(Note that these are not member functions.)

LHASH_EXPORT std::unique_ptr< HashercreateHasherForType (Type type)
 Creates an appropriate Hasher for the given Type. More...
 
LHASH_EXPORT std::string hash (Type type, const char *input, std::size_t length)
 Calculates a hash value for the input data using a hasher appropriate for the desired type. More...
 
LHASH_EXPORT std::string hash (Type type, std::string_view input)
 Calculates a hash value for the input data using a hasher appropriate for the desired type. More...
 

Detailed Description

A base class representing an object that calculates a hash function.

You should call the update() method with any data that needs to be hashed, and then call getHash() to finalize the internal calculations and retrieve the final hash value.

You shouldn't call update() again after calling getHash() .

You should use this class by either creating one of the hash function-specific supertypes, or through createHasherForType() , such as:

auto hasher = limes::hash::createHasherForType (limes::hash::Type::sha256);
hasher->update (get_some_data());
std::cout << hasher->getHash() << std::endl;
sha256
An SHA256 hash.
Definition: lhashes_hash.h:44
See also
MD5, SHA1, SHA224, SHA256, SHA384, SHA512

Definition at line 68 of file lhashes_hash.h.

Constructor & Destructor Documentation

◆ ~Hasher()

virtual limes::hash::Hasher::~Hasher ( )
virtualdefault

Destructor.

Member Function Documentation

◆ getHash()

virtual std::string limes::hash::Hasher::getHash ( )
pure virtual

Retrieves the calculated hash value as a string.

You shouldn't call update() again after calling this function.

Precondition
update() should have been called at least once before calling this function.
Postcondition
update() should not be called again after calling this function.
Returns
The hash string for all of the data that has been sent to update() . The string will be the length returned by getLengthOfHash() .

Implemented in limes::hash::SHA512, limes::hash::SHA384, limes::hash::SHA256, limes::hash::SHA224, limes::hash::SHA1, and limes::hash::MD5.

◆ getLengthOfHash()

virtual std::size_t limes::hash::Hasher::getLengthOfHash ( ) const
pure virtual

Returns the length of a hash string for this algorithm.

Implemented in limes::hash::SHA512, limes::hash::SHA384, limes::hash::SHA256, limes::hash::SHA224, limes::hash::SHA1, and limes::hash::MD5.

◆ update() [1/2]

virtual void limes::hash::Hasher::update ( const unsigned char *  input,
std::size_t  length 
)
pure virtual

Updates the internal state of the hasher with some new data.

Implemented in limes::hash::SHA224, limes::hash::MD5, limes::hash::SHA512, limes::hash::SHA384, limes::hash::SHA256, and limes::hash::SHA1.

◆ update() [2/2]

virtual void limes::hash::Hasher::update ( std::string_view  input)
virtual

Updates the internal state of the hasher with some new data.

Friends And Related Function Documentation

◆ createHasherForType()

LHASH_EXPORT std::unique_ptr< Hasher > createHasherForType ( Type  type)
related

Creates an appropriate Hasher for the given Type.

See also
hash()

◆ hash() [1/2]

LHASH_EXPORT std::string hash ( Type  type,
const char *  input,
std::size_t  length 
)
related

Calculates a hash value for the input data using a hasher appropriate for the desired type.

See also
createHasherForType()

◆ hash() [2/2]

LHASH_EXPORT std::string hash ( Type  type,
std::string_view  input 
)
related

Calculates a hash value for the input data using a hasher appropriate for the desired type.

See also
createHasherForType()

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