Hashing a file¶
blake3pp::digest d = blake3pp::hash_file("dataset.parquet");
That call opens the file with the fastest mechanism the platform offers, keeps several reads in flight, and hashes each window as it lands.
| OS | async engine | page-cache bypass |
|---|---|---|
| Linux | io_uring | O_DIRECT |
| Windows | IOCP | FILE_FLAG_NO_BUFFERING |
| macOS | GCD (libdispatch) | F_NOCACHE |
Each of those degrades on its own at run time. Without direct I/O the pipeline falls back to buffered async reads, then to synchronous reads, then to stdio. Nothing needs configuring for that to happen.
Throwing or not¶
Every entry point comes in two forms, following the standard library:
auto d = blake3pp::hash_file(path); // throws std::system_error
std::error_code ec;
auto d2 = blake3pp::hash_file(path, ec); // reports through ec
Paths are std::filesystem::path. A path-like type from another library
is accepted structurally if it has a native() observer, so a
Boost.Filesystem path compiles without blake3pp knowing Boost exists.
Feeding a hasher instead¶
hash_file() is the one-shot form. update_file() is the primitive
under it, and it streams a file into a hasher you own:
blake3pp::hasher all;
for (const auto& part : parts) {
blake3pp::update_file(all, part);
}
auto d = all.finalize();
Because the hasher belongs to the caller, its mode and all of its finalize forms apply to file input. That makes a keyed hash of a file, or a seekable stream derived from a file's bytes, the same one-liner.
Running it¶
With no arguments it generates two files in the temporary directory and hashes those. Given paths, it hashes them instead.
cmake --build build --target blake3pp_example_05_hash_a_file
./build/examples/blake3pp_example_05_hash_a_file [paths...]
Next¶
- 06-multi-core adds a scheduler, which is what takes the file pipeline to full speed.
The whole program¶
// Hashing files. The pipeline uses the platform's own async read
// mechanism and bypasses the page cache where it can.
#include <blake3pp/blake3pp.hpp>
#include <array>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <system_error>
#include <vector>
namespace {
// Two files to hash, so the example runs anywhere without arguments.
std::filesystem::path write_demo_file(const std::filesystem::path& dir,
const char* name, char fill,
std::size_t bytes) {
const std::filesystem::path path = dir / name;
std::ofstream out(path, std::ios::binary);
const std::vector<char> block(64 * 1024, fill);
for (std::size_t written = 0; written < bytes; written += block.size()) {
out.write(block.data(), static_cast<std::streamsize>(block.size()));
}
return path;
}
} // namespace
int main(int argc, char** argv) {
// Hash whatever the caller named, or a pair of generated files.
if (argc > 1) {
for (int i = 1; i < argc; ++i) {
std::cout << blake3pp::hash_file(argv[i]).to_hex() << " " << argv[i]
<< '\n';
}
return 0;
}
const std::filesystem::path dir =
std::filesystem::temp_directory_path() / "blake3pp-example-05";
std::filesystem::create_directories(dir);
const std::filesystem::path a = write_demo_file(dir, "a.bin", 'a', 4 << 20);
const std::filesystem::path b = write_demo_file(dir, "b.bin", 'b', 1 << 20);
// The one-shot form. Paths are std::filesystem::path, and this
// overload throws std::system_error on an I/O failure.
std::cout << "hash_file(a.bin)\n " << blake3pp::hash_file(a).to_hex() << '\n';
// Every entry point also has a std::error_code form, mirroring the
// standard library, for callers who would rather branch than catch.
std::error_code ec;
const blake3pp::digest d = blake3pp::hash_file(dir / "does-not-exist", ec);
if (ec) {
std::cout << "\nhash_file() on a missing file reports\n " << ec.message()
<< '\n';
}
// update_file() is the primitive underneath: it streams a file into a
// hasher you own. Because the hasher is yours, its mode and all of its
// finalize forms apply to file input, and several files hash as one
// message.
blake3pp::hasher both;
blake3pp::update_file(both, a);
blake3pp::update_file(both, b);
std::cout << "\nboth files as one message\n " << both.finalize().to_hex()
<< '\n';
// The same file through a keyed hasher gives an authenticated manifest
// entry: one that needs the key to produce, not only to check.
const std::array<std::byte, blake3pp::key_size> key{};
blake3pp::hasher mac = blake3pp::hasher::keyed(key);
blake3pp::update_file(mac, a);
std::cout << "\na keyed hash of a.bin\n " << mac.finalize().to_hex() << '\n';
std::filesystem::remove_all(dir);
(void)d;
}