Crypto module

Hash

Incremental cryptographic hash engine wrapping OpenSSL EVP digest functions.

Hash

#include <icy/crypto/hash.h>

Incremental cryptographic hash engine wrapping OpenSSL EVP digest functions.

Construct with an algorithm name recognized by OpenSSL (e.g. "sha256", "md5"). Feed data with one or more calls to update(), then call digest() or digestStr() to finalize and retrieve the result. Call reset() to reuse the engine for a new computation without reallocating the context.

Public Methods

ReturnNameDescription
HashConstructs a Hash engine for the given algorithm.
~HashDestroys the Hash engine and releases OpenSSL resources.
voidupdateFeeds a single character into the digest computation.
voidupdateFeeds a string view into the digest computation.
voidupdateFeeds a raw memory buffer into the digest computation.
const ByteVec &digestFinalizes the digest computation and returns the raw binary result.
std::stringdigestStrFinalizes the digest computation and returns the result as a raw binary string (not hex-encoded). Use icy::hex::encode() on digest() if you need a printable representation.
voidresetResets the digest context and clears the cached result, allowing the engine to be reused for a new computation with the same algorithm.
const std::string &algorithm constReturns the algorithm name this engine was constructed with.

Hash

Hash(const std::string & algorithm)

Constructs a Hash engine for the given algorithm.

Parameters

  • algorithm OpenSSL digest name (e.g. "sha256", "sha1", "md5").

Exceptions

  • std::runtime_error if the algorithm is not recognized by OpenSSL.

~Hash

~Hash()

Destroys the Hash engine and releases OpenSSL resources.


update

void update(char data)

Feeds a single character into the digest computation.

Parameters

  • data The byte to hash.

update

void update(std::string_view data)

Feeds a string view into the digest computation.

Parameters

  • data The data to hash.

update

void update(const void * data, size_t length)

Feeds a raw memory buffer into the digest computation.

This method may be called multiple times for streaming large inputs.

Parameters

  • data Pointer to the input buffer.

  • length Number of bytes to hash from data.


digest

const ByteVec & digest()

Finalizes the digest computation and returns the raw binary result.

The result is computed on the first call and cached; subsequent calls return the same value without recomputing. Call reset() before reusing the engine for a new computation.

Returns

Reference to the internal byte vector containing the digest.


digestStr

std::string digestStr()

Finalizes the digest computation and returns the result as a raw binary string (not hex-encoded). Use icy::hex::encode() on digest() if you need a printable representation.

Returns

Binary digest as a std::string.


reset

void reset()

Resets the digest context and clears the cached result, allowing the engine to be reused for a new computation with the same algorithm.


algorithm

const

const std::string & algorithm() const

Returns the algorithm name this engine was constructed with.

Returns

OpenSSL digest name string (e.g. "sha256").

Protected Attributes

ReturnNameDescription
EvpMdCtxPtr_ctx
const EVP_MD *_md
crypto::ByteVec_digest
std::string_algorithm

_ctx

EvpMdCtxPtr _ctx

_md

const EVP_MD * _md

_digest

crypto::ByteVec _digest

_algorithm

std::string _algorithm