#include <genesis/utils/tools/hash/sha256.hpp>
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 All rights reserved. er.g ay@a3 .epf l.ch
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- 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.
- 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 &&)=default | |
SHA256 (SHA256 const &)=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... | |
SHA256 & | operator= (SHA256 &&)=default |
SHA256 & | operator= (SHA256 const &)=default |
void | update (char const *input, size_t length) |
void | update (std::istream &is) |
void | update (std::shared_ptr< BaseInputSource > source) |
void | update (std::string const &s) |
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) |
SHA256 | ( | ) |
Initialize the object for use.
Definition at line 112 of file sha256.cpp.
|
default |
void clear | ( | ) |
Reset to initial state, that is, delete any intermediate input from update() calls.
Definition at line 187 of file sha256.cpp.
|
static |
Definition at line 135 of file sha256.cpp.
SHA256::DigestType final_digest | ( | ) |
Finish the calculation, prepare the object for next use, and return the digest.
Definition at line 245 of file sha256.cpp.
std::string final_hex | ( | ) |
Finish the calculation, prepare the object for next use, and return the hash.
Definition at line 239 of file sha256.cpp.
|
static |
Definition at line 154 of file sha256.cpp.
|
static |
Calculate the hash digest for the content of an input source.
Definition at line 128 of file sha256.cpp.
|
static |
Calculate the checksum for the content of an input source.
Definition at line 121 of file sha256.cpp.
void update | ( | char const * | input, |
size_t | length | ||
) |
Definition at line 232 of file sha256.cpp.
void update | ( | std::istream & | is | ) |
Definition at line 215 of file sha256.cpp.
void update | ( | std::shared_ptr< BaseInputSource > | source | ) |
Definition at line 192 of file sha256.cpp.
void update | ( | std::string const & | s | ) |
Definition at line 210 of file sha256.cpp.
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.
|
static |
Definition at line 110 of file sha256.hpp.
|
static |
Definition at line 111 of file sha256.hpp.