Class thl::net::Sha256#

class Sha256#

SHA-256 (FIPS 180-4).

Implemented in plain C++ rather than against CommonCrypto / BCrypt / OpenSSL on purpose: it removes a whole platform matrix from a component that already needs one for HTTP, and it keeps the library free of a system crypto dependency. Throughput is a few hundred MB/s, which is irrelevant next to the download it verifies.

Not real-time safe and not intended to be — this runs on a worker thread after a transfer.

Public Types

using Digest = std::array<std::uint8_t, 32>#

Raw digest, 32 bytes.

Public Functions

Sha256()#
void update(const void *data, std::size_t size)#

Feed more bytes. May be called any number of times before finish().

Digest finish()#

Finish and return the digest.

The object must not be reused afterwards without reset().

void reset()#

Return to the initial state so the object can hash something else.

Public Static Functions

static std::string to_hex(const Digest &digest)#

Lowercase hex, 64 characters — the form the manifests use.

static Digest of(const void *data, std::size_t size)#

Convenience: hash a whole buffer.

static FileResult of_file(const std::filesystem::path &path)#
static bool matches_hex(const Digest &digest, std::string_view hex)#

Case-insensitive comparison of a digest against a hex string.

Returns false for malformed input rather than throwing, since the string comes from a server.

struct FileResult#

Collaboration diagram for thl::net::Sha256::FileResult:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "2" [label="std::array< std::uint8_t, 32 >" tooltip="std::array< std::uint8_t, 32 >"] "1" [label="thl::net::Sha256::FileResult" tooltip="thl::net::Sha256::FileResult" fillcolor="#BFBFBF"] "1" -> "2" [dir=forward tooltip="usage"] }

Convenience: hash a file in chunks, so size is bounded by the buffer and not by the file.

Returns an empty optional-shaped result — an all-zero digest with ok == false — when the file cannot be read.

Public Members

Digest m_digest = {}#
bool m_ok = false#