From 855affd9b681d98f248009ddb2c1abe987029f72 Mon Sep 17 00:00:00 2001 From: Oliver Reiche Date: Wed, 15 Jun 2022 18:51:47 +0200 Subject: Crypto: Refactor hash computation ... by renaming HashGenerator to (incremental) Hasher and dropping support for Git/MD5 hashes. The Hasher does not expose the actual hash implementation. --- test/buildtool/crypto/hasher.test.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/buildtool/crypto/hasher.test.cpp (limited to 'test/buildtool/crypto/hasher.test.cpp') diff --git a/test/buildtool/crypto/hasher.test.cpp b/test/buildtool/crypto/hasher.test.cpp new file mode 100644 index 00000000..01a5cb12 --- /dev/null +++ b/test/buildtool/crypto/hasher.test.cpp @@ -0,0 +1,28 @@ +#include "catch2/catch.hpp" +#include "src/buildtool/crypto/hasher.hpp" + +template +void test_increment_hash(std::string const& bytes, std::string const& result) { + Hasher hasher{type}; + hasher.Update(bytes.substr(0, bytes.size() / 2)); + hasher.Update(bytes.substr(bytes.size() / 2)); + auto digest = std::move(hasher).Finalize(); + CHECK(digest.HexString() == result); +} + +TEST_CASE("Hasher", "[crypto]") { + std::string bytes{"test"}; + + SECTION("SHA-1") { + // same as: echo -n test | sha1sum + test_increment_hash( + bytes, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); + } + + SECTION("SHA-256") { + // same as: echo -n test | sha256sum + test_increment_hash( + bytes, + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + } +} -- cgit v1.2.3