A toolkit for working with phylogenetic data.
v0.24.0
SHA256 Class Reference

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

Detailed Description

Calculate SHA256 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 http://www.zedwood.com/article/cpp-sha256-function, which itself is based on Olivier Gay's version, and was published with a Modified BSD License:

License

Updated to C++, zedwood.com 2012 Based on Olivier Gay's version See Modified BSD License below:

FIPS 180-2 SHA-224/256/384/512 implementation Issue date: 04/30/2005 http://www.ouah.org/ogay/sha2/

Copyright (C) 2005, 2007 Olivier Gay olivi.nosp@m.er.g.nosp@m.ay@a3.nosp@m..epf.nosp@m.l.ch All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

See also Acknowledgements.

Definition at line 102 of file sha256.hpp.

Public Member Functions

 SHA256 ()
 Initialize the object for use. More...
 
 SHA256 (SHA256 const &)=default
 
 SHA256 (SHA256 &&)=default
 
 ~SHA256 ()=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...
 
SHA256operator= (SHA256 const &)=default
 
SHA256operator= (SHA256 &&)=default
 
void update (std::shared_ptr< BaseInputSource > source)
 
void update (std::string const &s)
 
void update (std::istream &is)
 
void update (char const *input, size_t length)
 

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, 8 >
 Store a SHA256 digest. More...
 

Static Public Attributes

static const size_t BlockSize = ( 512 / 8 )
 
static const size_t DigestSize = ( 256 / 8)
 

Constructor & Destructor Documentation

◆ SHA256() [1/3]

SHA256 ( )

Initialize the object for use.

Definition at line 112 of file sha256.cpp.

◆ ~SHA256()

~SHA256 ( )
default

◆ SHA256() [2/3]

SHA256 ( SHA256 const &  )
default

◆ SHA256() [3/3]

SHA256 ( SHA256 &&  )
default

Member Function Documentation

◆ clear()

void clear ( )

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

Definition at line 192 of file sha256.cpp.

◆ digest_to_hex()

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

Definition at line 135 of file sha256.cpp.

◆ final_digest()

SHA256::DigestType final_digest ( )

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

Definition at line 250 of file sha256.cpp.

◆ final_hex()

std::string final_hex ( )

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

Definition at line 244 of file sha256.cpp.

◆ hex_to_digest()

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

Definition at line 154 of file sha256.cpp.

◆ operator=() [1/2]

SHA256& operator= ( SHA256 const &  )
default

◆ operator=() [2/2]

SHA256& operator= ( SHA256 &&  )
default

◆ read_digest()

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

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

Definition at line 128 of file sha256.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 121 of file sha256.cpp.

◆ update() [1/4]

void update ( std::shared_ptr< BaseInputSource source)

Definition at line 197 of file sha256.cpp.

◆ update() [2/4]

void update ( std::string const &  s)

Definition at line 215 of file sha256.cpp.

◆ update() [3/4]

void update ( std::istream &  is)

Definition at line 220 of file sha256.cpp.

◆ update() [4/4]

void update ( char const *  input,
size_t  length 
)

Definition at line 237 of file sha256.cpp.

Member Typedef Documentation

◆ DigestType

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

Store a SHA256 digest.

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

Definition at line 120 of file sha256.hpp.

Member Data Documentation

◆ BlockSize

const size_t BlockSize = ( 512 / 8 )
static

Definition at line 110 of file sha256.hpp.

◆ DigestSize

const size_t DigestSize = ( 256 / 8)
static

Definition at line 111 of file sha256.hpp.


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