diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2022-06-15 18:51:47 +0200 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2022-06-20 15:23:02 +0200 |
commit | 855affd9b681d98f248009ddb2c1abe987029f72 (patch) | |
tree | 6d4dbfc2c99020772313f381d2f793950d2b03f4 /test/buildtool/crypto/hasher.test.cpp | |
parent | 391d982f2fbd98a2973f14e0b5969f66c2abd756 (diff) | |
download | justbuild-855affd9b681d98f248009ddb2c1abe987029f72.tar.gz |
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.
Diffstat (limited to 'test/buildtool/crypto/hasher.test.cpp')
-rw-r--r-- | test/buildtool/crypto/hasher.test.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
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 <Hasher::HashType type> +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<Hasher::HashType::SHA1>( + bytes, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); + } + + SECTION("SHA-256") { + // same as: echo -n test | sha256sum + test_increment_hash<Hasher::HashType::SHA256>( + bytes, + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + } +} |