Class Sha256

Class Documentation

class Sha256

A simple class to represent a SHA256 checksum.

Public Functions

Sha256() = default

Instantiate a Sha256 with an uninitialized hash value.

explicit Sha256(const sha256_hash_t &hash)

Initialize a Sha256 from an array.

Parameters:

hash[in] – An array of words containing the hash value.

explicit Sha256(const std::string &hex_str)

Initialize a Sha256 from a hex string.

Parameters:

hex_str[in] – A hex string containing the hash value.

std::string str() const

Returns a hex string of the hash.

Returns:

The hex string.

bool operator==(const Sha256 &sha256_b) const

Compare two hash values for equality.

Returns:

true if the hashes are equal, false otherwise.

bool operator!=(const Sha256 &sha256_b) const

Compare two hash values for inequality.

Returns:

true if the hashes are not equal, false otherwise.

inline const unsigned char *bytes() const

Returns a pointer to the hash data.

Returns:

A pointer to the hash data.

Public Static Functions

static Sha256 hash_file(const std::pair<std::istream&, size_t> &file, std::pair<const void*, size_t> prefix = {nullptr, 0})

Creates a Sha256 from an input stream.

Parameters:
  • file[in] – A pair containing an istream and an offset to start from.

  • prefix[in] – An optional buffer to hash prior to the istream contents.

Returns:

The Sha256 of the stream and prefix.

static Sha256 hash_files(const std::vector<std::pair<std::istream&, size_t>> &files, std::pair<const void*, size_t> prefix = {nullptr, 0})

Creates a Sha256 from an input stream.

Parameters:
  • files[in] – A vector of pair containing istreams and offsets to start from.

  • prefix[in] – An optional buffer to hash prior to the ifstream contents.

Returns:

The Sha256 of the stream and prefix.

static Sha256 hash_blobs(const std::vector<std::pair<const void*, size_t>> &blobs)

Creates a Sha256 from a vector of buffers.

Parameters:

blobs[in] – A vector of pair containing pointers to buffers’ data and their sizes.

Returns:

The Sha256 of the buffers.