From e57b284ffcdab60e8f8a5c52ffcb2608867aefe1 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 6 Aug 2025 17:53:24 +0300 Subject: [PATCH] Add constexpr md5 hash implementation --- libs/util/include/psemek/util/md5.hpp | 12 ++ libs/util/include/psemek/util/md5_inl.hpp | 202 ++++++++++++++++++++++ libs/util/source/md5.cpp | 12 ++ 3 files changed, 226 insertions(+) create mode 100644 libs/util/include/psemek/util/md5.hpp create mode 100644 libs/util/include/psemek/util/md5_inl.hpp create mode 100644 libs/util/source/md5.cpp diff --git a/libs/util/include/psemek/util/md5.hpp b/libs/util/include/psemek/util/md5.hpp new file mode 100644 index 00000000..9d726a7e --- /dev/null +++ b/libs/util/include/psemek/util/md5.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include +#include + +namespace psemek::util +{ + + std::array md5_hash(std::string_view data); + +} diff --git a/libs/util/include/psemek/util/md5_inl.hpp b/libs/util/include/psemek/util/md5_inl.hpp new file mode 100644 index 00000000..80c4917d --- /dev/null +++ b/libs/util/include/psemek/util/md5_inl.hpp @@ -0,0 +1,202 @@ +#pragma once + +#include +#include +#include + +namespace psemek::util +{ + + // Constexpr MD5 hash implementation. + // Partially based on https://github.com/Wodann/constexpr-md5-cpp/blob/master/include/md5.h + + namespace md5_detail + { + + constexpr std::uint32_t f(std::uint32_t b, std::uint32_t c, std::uint32_t d) + { + return (b & c) | (~b & d); + } + + constexpr std::uint32_t g(std::uint32_t b, std::uint32_t c, std::uint32_t d) + { + return (b & d) | (c & ~d); + } + + constexpr std::uint32_t h(std::uint32_t b, std::uint32_t c, std::uint32_t d) + { + return b ^ c ^ d; + } + + constexpr std::uint32_t i(std::uint32_t b, std::uint32_t c, std::uint32_t d) + { + return c ^ (b | ~d); + } + + using Fn = uint32_t(*)(uint32_t, uint32_t, uint32_t); + + constexpr Fn F[4] = { f, g, h, i }; + + constexpr std::uint32_t G[64] = + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, + 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, + 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 + }; + + constexpr std::uint32_t K[64] = + { + 0xd76aa478u, 0xe8c7b756u, 0x242070dbu, 0xc1bdceeeu, + 0xf57c0fafu, 0x4787c62au, 0xa8304613u, 0xfd469501u, + 0x698098d8u, 0x8b44f7afu, 0xffff5bb1u, 0x895cd7beu, + 0x6b901122u, 0xfd987193u, 0xa679438eu, 0x49b40821u, + 0xf61e2562u, 0xc040b340u, 0x265e5a51u, 0xe9b6c7aau, + 0xd62f105du, 0x02441453u, 0xd8a1e681u, 0xe7d3fbc8u, + 0x21e1cde6u, 0xc33707d6u, 0xf4d50d87u, 0x455a14edu, + 0xa9e3e905u, 0xfcefa3f8u, 0x676f02d9u, 0x8d2a4c8au, + 0xfffa3942u, 0x8771f681u, 0x6d9d6122u, 0xfde5380cu, + 0xa4beea44u, 0x4bdecfa9u, 0xf6bb4b60u, 0xbebfbc70u, + 0x289b7ec6u, 0xeaa127fau, 0xd4ef3085u, 0x04881d05u, + 0xd9d4d039u, 0xe6db99e5u, 0x1fa27cf8u, 0xc4ac5665u, + 0xf4292244u, 0x432aff97u, 0xab9423a7u, 0xfc93a039u, + 0x655b59c3u, 0x8f0ccc92u, 0xffeff47du, 0x85845dd1u, + 0x6fa87e4fu, 0xfe2ce6e0u, 0xa3014314u, 0x4e0811a1u, + 0xf7537e82u, 0xbd3af235u, 0x2ad7d2bbu, 0xeb86d391u, + }; + + constexpr std::uint32_t S[16] = + { + 7, 12, 17, 22, + 5, 9, 14, 20, + 4, 11, 16, 23, + 6, 10, 15, 21 + }; + + constexpr char PADDING[64] = + { + -128, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + }; + + constexpr std::uint32_t rotate(std::uint32_t x, std::uint32_t n) + { + return (x << n) | (x >> (32 - n)); + } + + template + constexpr std::uint32_t t(F f, std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d, std::uint32_t x, std::uint32_t s, std::uint32_t ac) + { + return b + rotate(a + f(b, c, d) + x + ac, s); + } + + constexpr uint32_t to_uint32(unsigned char const * data) + { + return 0 + | (static_cast(data[0]) << 0) + | (static_cast(data[1]) << 8) + | (static_cast(data[2]) << 16) + | (static_cast(data[3]) << 24) + ; + } + + + struct hasher + { + std::uint8_t buffer[64]; + std::uint32_t state[4] = { 0x67452301u, 0xefcdab89u, 0x98badcfeu, 0x10325476u }; + std::uint32_t nl = 0; + std::uint32_t nh = 0; + + constexpr void append(std::string_view data) + { + std::uint32_t input[16]; + + std::uint32_t k = (nl >> 3) & 0x3f; + + std::uint32_t length = data.size(); + nl += length << 3; + if (nl < length << 3) + nh += 1; + nh += length >> 29; + + for (char c : data) + { + buffer[k++] = static_cast(static_cast(c) + 256); + + if (k == 0x40) + { + for (int i = 0; i < 16; ++i) + input[i] = to_uint32(buffer + i * 4); + transform(input); + k = 0; + } + } + } + + constexpr void transform(std::uint32_t const (&input)[16]) + { + std::uint32_t a = state[0], b = state[1], c = state[2], d = state[3]; + for (uint32_t r = 0; r < 4; ++r) + { + const auto g = G + r * 16; + const auto s = S + r * 4; + const auto k = K + r * 16; + + for (std::uint32_t i = 0; i < 16; ++i) + { + const auto new_b = t(F[r], a, b, c, d, input[g[i]], s[i % 4], k[i]); + a = d; + d = c; + c = b; + b = new_b; + } + } + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + constexpr std::array result() + { + std::uint32_t input[16]; + + const auto k = ((nl >> 3) & 0x3f); + input[14] = nl; + input[15] = nh; + + append(std::string_view(PADDING, k < 56 ? 56 - k : 120 - k)); + + for (int i = 0; i < 14; ++i) { + input[i] = to_uint32(buffer + i * 4); + } + transform(input); + + return {state[0], state[1], state[2], state[3]}; + } + }; + + } + + namespace md5_inl + { + + using hasher = md5_detail::hasher; + + constexpr auto hash(std::string_view data) + { + hasher h; + h.append(data); + return h.result(); + } + + } + +} diff --git a/libs/util/source/md5.cpp b/libs/util/source/md5.cpp new file mode 100644 index 00000000..dbea84cb --- /dev/null +++ b/libs/util/source/md5.cpp @@ -0,0 +1,12 @@ +#include +#include + +namespace psemek::util +{ + + std::array md5_hash(std::string_view data) + { + return md5_inl::hash(data); + } + +}