diff --git a/include/boost/hash2/sha2.hpp b/include/boost/hash2/sha2.hpp index 31e06e9..98493ed 100644 --- a/include/boost/hash2/sha2.hpp +++ b/include/boost/hash2/sha2.hpp @@ -37,13 +37,11 @@ struct sha2_base static constexpr int N = M; unsigned char buffer_[ N ] = {}; - std::size_t m_; // == n_ % N + std::size_t m_ = 0; // == n_ % N - std::uint64_t n_; + std::uint64_t n_ = 0; - BOOST_CXX14_CONSTEXPR sha2_base(): m_( 0 ), n_( 0 ) - { - } + constexpr sha2_base() = default; void update( void const* pv, std::size_t n ) { @@ -540,7 +538,6 @@ class sha2_512 : detail::sha2_512_base } } - BOOST_CXX14_CONSTEXPR result_type result() { unsigned char bits[ 16 ] = { 0 }; diff --git a/test/Jamfile b/test/Jamfile index 6f49514..166a3b3 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -124,6 +124,7 @@ run sha1_cx_2.cpp ; run sha2.cpp ; run hmac_sha2.cpp ; run sha2_cx.cpp ; +run sha2_cx_2.cpp ; run ripemd.cpp ; run hmac_ripemd.cpp ; diff --git a/test/sha2_cx_2.cpp b/test/sha2_cx_2.cpp new file mode 100644 index 0000000..d69101b --- /dev/null +++ b/test/sha2_cx_2.cpp @@ -0,0 +1,83 @@ +// Copyright 2024 Christian Mazakas +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +#if defined(BOOST_MSVC) && BOOST_MSVC < 1920 +# pragma warning(disable: 4307) // integral constant overflow +#endif + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +#if defined(BOOST_NO_CXX14_CONSTEXPR) + +# define TEST_EQ(x1, x2) BOOST_TEST_EQ(x1, x2) + +#else + +# define TEST_EQ(x1, x2) BOOST_TEST_EQ(x1, x2); STATIC_ASSERT(x1 == x2) + +#endif + +BOOST_CXX14_CONSTEXPR unsigned char to_byte( char c ) +{ + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return 0xff; +} + +template +BOOST_CXX14_CONSTEXPR boost::hash2::digest digest_from_hex( char const (&str)[ N ] ) +{ + boost::hash2::digest dgst = {}; + auto* p = dgst.data(); + for( unsigned i = 0; i < M; ++i ) { + auto c1 = to_byte( str[ 2 * i ] ); + auto c2 = to_byte( str[ 2 * i + 1 ] ); + p[ i ] = ( c1 << 4 ) | c2; + } + return dgst; +} + +int main() +{ + using namespace boost::hash2; + + BOOST_CXX14_CONSTEXPR digest<32> r1 = digest_from_hex( "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ); + BOOST_CXX14_CONSTEXPR digest<28> r2 = digest_from_hex( "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" ); + BOOST_CXX14_CONSTEXPR digest<64> r3 = digest_from_hex( "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" ); + BOOST_CXX14_CONSTEXPR digest<48> r4 = digest_from_hex( "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" ); + BOOST_CXX14_CONSTEXPR digest<28> r5 = digest_from_hex( "6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4" ); + BOOST_CXX14_CONSTEXPR digest<32> r6 = digest_from_hex( "c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a" ); + + TEST_EQ( sha2_256().result(), r1 ); + TEST_EQ( sha2_256(0).result(), r1 ); + TEST_EQ( sha2_256(nullptr, 0).result(), r1 ); + + TEST_EQ( sha2_224().result(), r2 ); + TEST_EQ( sha2_224(0).result(), r2 ); + TEST_EQ( sha2_224(nullptr, 0).result(), r2 ); + + TEST_EQ( sha2_512().result(), r3 ); + TEST_EQ( sha2_512(0).result(), r3 ); + TEST_EQ( sha2_512(nullptr, 0).result(), r3 ); + + TEST_EQ( sha2_384().result(), r4 ); + TEST_EQ( sha2_384(0).result(), r4 ); + TEST_EQ( sha2_384(nullptr, 0).result(), r4 ); + + TEST_EQ( sha2_512_224().result(), r5 ); + TEST_EQ( sha2_512_224(0).result(), r5 ); + TEST_EQ( sha2_512_224(nullptr, 0).result(), r5 ); + + TEST_EQ( sha2_512_256().result(), r6 ); + TEST_EQ( sha2_512_256(0).result(), r6 ); + TEST_EQ( sha2_512_256(nullptr, 0).result(), r6 ); + + return boost::report_errors(); +}