Implementation of SHA-2 cryptographic hash functions which enables compile time evaluation of message digests. This can be useful e.g. when strings are used in lookup tables but you don't want those strings to be visible in executable code.
Please note that implementation is far from optimal. The primary goal was to enable compile time generation of digest.
Entire implementation is in a single header file include/sha2.hpp. It contains:
sha224_tclass that implements SHA-224 algorithmsha256_tclass that implements SHA-256 algorithmsha384_tclass that implements SHA-384 algorithmsha512_tclass that implements SHA-512 algorithmsha512_224_tclass that implements SHA-512/224 algorithmsha512_256_tclass that implements SHA-512/256 algorithm.
Constructor accepts UTF8 encoded string and digest is obtained by digest member function as an array of binary values:
#include <sha2.hpp>
#include <string_view>
using namespace jsribar::cryptography::sha2;
constexpr std::array<uint8_t, 32> digest256 = sha256_t{"abc"}.digest();
constexpr std::string_view str{ "xyz" };
constexpr sha512_t sha512{ str };
constexpr auto digest512 = sha512.digest(); Code has been successfully compiled with Visual Studio 2022 Version 17.12.3 and GCC 13.2.0.
tests directory contains unit tests. Unit tests use Catch2 v2.x framework. To compile and run unit tests in Visual Studio solution provided, adjust the include path or simply set the environment variable ThirParty to point to the parent directory inside which Catch2 framework is cloned.
- ‘SHA-2’ (2024). Wikipedia. Available at: https://en.wikipedia.org/wiki/SHA-2 (Accessed: 24 December 2024)
- ‘SHA256 Algorithm Explained’ (2024). Available at: https://sha256algorithm.com/ (Accessed: 24 December 2024)
- ‘Online Tools’ (2024). Available at: https://emn178.github.io/online-tools/ (Accessed: 24 December 2024)
- ‘Catch2’ (2024). Available at: https://github.com/catchorg/Catch2 (Accessed: 24 December 2024)