Files
Library/tests/standalone/Crypto/Crypto.cpp
faumaray 3851aeb703
CI / GCC 16 / C++26 / werror (push) Failing after 3m9s
CI / GCC 16 / C++26 / sanitizers (push) Failing after 10m15s
CI / GCC 16 / C++26 / default (push) Failing after 13m1s
Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-20 01:18:53 +03:00

78 lines
2.8 KiB
C++

#include <Void/Crypto/Crypto.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <Void/Text/Hex.hpp>
#include <array>
#include <fstream>
namespace
{
std::string hex(const Void::Crypto::Digest& digest)
{
return Void::Encoding::Hex::encode(digest);
}
} // namespace
Void::Testing::Test crypto_known_hash_vectors()
{
auto sha1 = Void::Crypto::sha1("abc");
auto sha256 = Void::Crypto::sha256("abc");
auto sha512 = Void::Crypto::sha512("abc");
Void::Testing::require(sha1.has_value());
Void::Testing::require(sha256.has_value());
Void::Testing::require(sha512.has_value());
Void::Testing::expect_eq(
hex(*sha1), std::string{"a9993e364706816aba3e25717850c26c9cd0d89d"});
Void::Testing::expect_eq(hex(*sha256),
std::string{"ba7816bf8f01cfea414140de5dae2223b0036"
"1a396177a9cb410ff61f20015ad"});
Void::Testing::expect_eq(
hex(*sha512),
std::string{
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a21"
"92992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"});
}
Void::Testing::Test crypto_streaming_and_file_hash_match()
{
auto hasher =
Void::Crypto::Hasher::create(Void::Crypto::HashAlgorithm::Sha256);
Void::Testing::require(hasher.has_value());
Void::Testing::require(hasher->update("ab").has_value());
Void::Testing::require(hasher->update("c").has_value());
auto streamed = hasher->finalize();
Void::Testing::require(streamed.has_value());
Void::Testing::expect(!hasher->update("again").has_value());
Void::FileSystem::TempDir temporary{"void_crypto_"};
const auto path = temporary / "data";
std::ofstream{path, std::ios::binary} << "abc";
auto file = Void::Crypto::sha256File(path);
Void::Testing::require(file.has_value());
Void::Testing::expect_eq(*file, *streamed);
}
Void::Testing::Test crypto_ed25519_rfc8032_vector()
{
const auto publicText = Void::Encoding::Hex::decode(
"d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a");
const auto signatureText = Void::Encoding::Hex::decode(
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155"
"5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b");
Void::Testing::require(publicText.has_value());
Void::Testing::require(signatureText.has_value());
const auto publicKey =
std::as_bytes(std::span{publicText->data(), publicText->size()});
const auto signature =
std::as_bytes(std::span{signatureText->data(), signatureText->size()});
auto valid = Void::Crypto::verifyEd25519({}, signature, publicKey);
Void::Testing::require(valid.has_value());
Void::Testing::expect(*valid);
const std::array message{std::byte{1}};
auto invalid = Void::Crypto::verifyEd25519(message, signature, publicKey);
Void::Testing::require(invalid.has_value());
Void::Testing::expect(!*invalid);
}