Skip to content

jsribar/ConstexprSHA-2

Repository files navigation

constexpr SHA-2

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.

Usage

Entire implementation is in a single header file include/sha2.hpp. It contains:

  • sha224_t class that implements SHA-224 algorithm
  • sha256_t class that implements SHA-256 algorithm
  • sha384_t class that implements SHA-384 algorithm
  • sha512_t class that implements SHA-512 algorithm
  • sha512_224_t class that implements SHA-512/224 algorithm
  • sha512_256_t class 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.

Unit tests

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.

References

Releases

No releases published

Packages

No packages published