A library for working with phylogenetic and population genetic data.
v0.32.0
SHA1 Class Reference

#include <genesis/utils/tools/hash/sha1.hpp>

Detailed Description

Calculate SHA1 hashes for strings and files.

After creating an object of this type, call update() with your input data as argument, as often as needed. Then, call final_hex() or final_digest() to obtain the hash and reset the object for reuse.

If you simply need the hash for a string or content of a file, use read_hex() or read_digest(), which are static shortcuts for the above using an input source. Use functions such as utils::from_file() and utils::from_string() to conveniently get an input source that can be used here.

The implementation is based on https://github.com/vog/sha1, which is 100% Public Domain, see also Acknowledgements.

Definition at line 64 of file sha1.hpp.

Public Member Functions

 SHA1 ()
 Initialize the object for use. More...
 
 SHA1 (SHA1 &&)=default
 
 SHA1 (SHA1 const &)=default
 
 ~SHA1 ()=default
 
void clear ()
 Reset to initial state, that is, delete any intermediate input from update() calls. More...
 
DigestType final_digest ()
 Finish the calculation, prepare the object for next use, and return the digest. More...
 
std::string final_hex ()
 Finish the calculation, prepare the object for next use, and return the hash. More...
 
SHA1operator= (SHA1 &&)=default
 
SHA1operator= (SHA1 const &)=default
 
void update (std::istream &is)
 Add the contents of a stream to the hash digest. More...
 
void update (std::shared_ptr< BaseInputSource > source)
 
void update (std::string const &s)
 Add the contents of a string to the hash digest. More...
 

Static Public Member Functions

static std::string digest_to_hex (DigestType const &digest)
 
static DigestType hex_to_digest (std::string const &hex)
 
static DigestType read_digest (std::shared_ptr< BaseInputSource > source)
 Calculate the hash digest for the content of an input source. More...
 
static std::string read_hex (std::shared_ptr< BaseInputSource > source)
 Calculate the checksum for the content of an input source. More...
 

Public Types

using DigestType = std::array< uint32_t, 5 >
 Store a SHA1 digest. More...
 

Static Public Attributes

static const size_t BlockBytes = BlockInts * 4
 
static const size_t BlockInts = 16
 

Constructor & Destructor Documentation

◆ SHA1() [1/3]

SHA1 ( )

Initialize the object for use.

Definition at line 56 of file sha1.cpp.

◆ ~SHA1()

~SHA1 ( )
default

◆ SHA1() [2/3]

SHA1 ( SHA1 const &  )
default

◆ SHA1() [3/3]

SHA1 ( SHA1 &&  )
default

Member Function Documentation

◆ clear()

void clear ( )

Reset to initial state, that is, delete any intermediate input from update() calls.

Definition at line 124 of file sha1.cpp.

◆ digest_to_hex()

std::string digest_to_hex ( SHA1::DigestType const &  digest)
static

Definition at line 79 of file sha1.cpp.

◆ final_digest()

SHA1::DigestType final_digest ( )

Finish the calculation, prepare the object for next use, and return the digest.

Definition at line 182 of file sha1.cpp.

◆ final_hex()

std::string final_hex ( )

Finish the calculation, prepare the object for next use, and return the hash.

Definition at line 176 of file sha1.cpp.

◆ hex_to_digest()

SHA1::DigestType hex_to_digest ( std::string const &  hex)
static

Definition at line 91 of file sha1.cpp.

◆ operator=() [1/2]

SHA1& operator= ( SHA1 &&  )
default

◆ operator=() [2/2]

SHA1& operator= ( SHA1 const &  )
default

◆ read_digest()

SHA1::DigestType read_digest ( std::shared_ptr< BaseInputSource source)
static

Calculate the hash digest for the content of an input source.

Definition at line 72 of file sha1.cpp.

◆ read_hex()

std::string read_hex ( std::shared_ptr< BaseInputSource source)
static

Calculate the checksum for the content of an input source.

Definition at line 65 of file sha1.cpp.

◆ update() [1/3]

void update ( std::istream &  is)

Add the contents of a stream to the hash digest.

Definition at line 159 of file sha1.cpp.

◆ update() [2/3]

void update ( std::shared_ptr< BaseInputSource source)

Definition at line 129 of file sha1.cpp.

◆ update() [3/3]

void update ( std::string const &  s)

Add the contents of a string to the hash digest.

Definition at line 153 of file sha1.cpp.

Member Typedef Documentation

◆ DigestType

using DigestType = std::array< uint32_t, 5 >

Store a SHA1 digest.

This type can be used for storing hashes instead of the hex representation. It only needs 20 bytes instead of 40 bytes for the hex. As it is a std::array, this type can be compared using normal ==.

Definition at line 83 of file sha1.hpp.

Member Data Documentation

◆ BlockBytes

const size_t BlockBytes = BlockInts * 4
static

Definition at line 74 of file sha1.hpp.

◆ BlockInts

const size_t BlockInts = 16
static

Definition at line 73 of file sha1.hpp.


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