One-shot hashing¶
The shortest path from bytes to a digest, and what you can do with the digest once you have it.
blake3pp::digest d = blake3pp::hash("hello world");
std::cout << d.to_hex() << '\n';
blake3pp::hash takes a std::string_view or anything convertible to
std::span<const std::byte>, so strings, vectors and arrays all go in
without a cast.
blake3pp::digest is a value type. It compares with ==, copies, and
round-trips through hex:
to_hex()returns astd::stringof 64 lowercase characters.to_hex_chars()returns astd::array<char, 65>and never allocates.digest::from_hex()parses, returningstd::optional<digest>.matches()compares against hex in one step.
Comparison is constant time in every form, which is the safe default for a value that gets compared against untrusted input. Hex that does not parse is treated as "no match" rather than as an error.
Running it¶
cmake --build build --target blake3pp_example_01_hello_hash
./build/examples/blake3pp_example_01_hello_hash
Next¶
- 02-incremental hashes data that arrives in pieces.
The whole program¶
// One-shot hashing, and what a digest is.
//
// Build this on its own with:
// c++ -std=c++20 -I../../include main.cpp -L. -lblake3pp
#include <blake3pp/blake3pp.hpp>
#include <array>
#include <cstddef>
#include <iostream>
#include <optional>
#include <vector>
int main() {
// The simplest call there is. A string_view goes straight in.
const blake3pp::digest d = blake3pp::hash("hello world");
std::cout << "hash(\"hello world\")\n " << d.to_hex() << "\n\n";
// Anything span-like works the same way.
const std::vector<std::byte> payload(4096, std::byte{0x42});
std::cout << "hash(4 KiB of 0x42)\n "
<< blake3pp::hash(payload).to_hex() << "\n\n";
// A digest is a regular value type, so it compares and copies like one.
std::cout << std::boolalpha;
std::cout << "the same input hashes to the same digest: "
<< (d == blake3pp::hash("hello world")) << '\n';
// It also round-trips through hex.
const std::optional<blake3pp::digest> parsed =
blake3pp::digest::from_hex(d.to_hex());
std::cout << "and survives a trip through hex: " << (parsed == d) << '\n';
// Checking a digest that arrived from somewhere else is one call. The
// comparison is constant time, and hex that does not parse is no match
// rather than an error.
std::cout << "matches() accepts the right hex: "
<< d.matches("d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24")
<< '\n';
std::cout << "matches() rejects anything else: "
<< d.matches("not hex at all") << '\n';
// For hot paths and C interop there is an allocation-free hex form: 64
// characters plus a terminating NUL.
const std::array<char, 65> hex = d.to_hex_chars();
std::cout << "\nto_hex_chars() does not allocate: " << hex.data() << '\n';
}