A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SHA256 Class Reference

#include <genesis/utils/tools/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 from_string() or from_file(), which are static shortcuts for the above.

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:

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 96 of file sha256.hpp.

Public Member Functions

 SHA256 ()
 Initialize the object for use. More...
 
 SHA256 (SHA256 const &)=default
 
 SHA256 (SHA256 &&)=default
 
 ~SHA256 ()=default
 
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::string const &s)
 Add the contents of a string to the hash digest. More...
 
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 from_file_digest (std::string const &filename)
 Calculate the hash digest for the content of a file, given its path. More...
 
static std::string from_file_hex (std::string const &filename)
 Calculate the checksum for the content of a file, given its path. More...
 
static DigestType from_string_digest (std::string const &input)
 Calculate the hash digest for the content of a string. More...
 
static std::string from_string_hex (std::string const &input)
 Calculate the checksum for the content of a string. More...
 
static DigestType hex_to_digest (std::string const &hex)
 

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 ( )

Initialize the object for use.

Definition at line 113 of file sha256.cpp.

~SHA256 ( )
default
SHA256 ( SHA256 const &  )
default
SHA256 ( SHA256 &&  )
default

Member Function Documentation

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

Definition at line 233 of file sha256.cpp.

SHA256::DigestType final_digest ( )

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

Definition at line 166 of file sha256.cpp.

std::string final_hex ( )

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

Definition at line 157 of file sha256.cpp.

SHA256::DigestType from_file_digest ( std::string const &  filename)
static

Calculate the hash digest for the content of a file, given its path.

Definition at line 205 of file sha256.cpp.

std::string from_file_hex ( std::string const &  filename)
static

Calculate the checksum for the content of a file, given its path.

Definition at line 194 of file sha256.cpp.

SHA256::DigestType from_string_digest ( std::string const &  input)
static

Calculate the hash digest for the content of a string.

Definition at line 226 of file sha256.cpp.

std::string from_string_hex ( std::string const &  input)
static

Calculate the checksum for the content of a string.

Definition at line 216 of file sha256.cpp.

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

Definition at line 252 of file sha256.cpp.

SHA256& operator= ( SHA256 const &  )
default
SHA256& operator= ( SHA256 &&  )
default
void update ( std::string const &  s)

Add the contents of a string to the hash digest.

Definition at line 125 of file sha256.cpp.

void update ( std::istream &  is)

Definition at line 130 of file sha256.cpp.

void update ( char const *  input,
size_t  length 
)

Definition at line 147 of file sha256.cpp.

Member Typedef Documentation

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 114 of file sha256.hpp.

Member Data Documentation

const size_t BlockSize = ( 512 / 8 )
static

Definition at line 104 of file sha256.hpp.

const size_t DigestSize = ( 256 / 8)
static

Definition at line 105 of file sha256.hpp.


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