mirror of
https://github.com/boostorg/hash2.git
synced 2026-01-26 18:42:13 +00:00
117 lines
4.2 KiB
C++
117 lines
4.2 KiB
C++
// Copyright 2024 Christian Mazakas
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
// https://www.boost.org/LICENSE_1_0.txt
|
|
|
|
#include <boost/hash2/sha3.hpp>
|
|
#include <boost/hash2/digest.hpp>
|
|
#include <boost/hash2/detail/config.hpp>
|
|
#include <boost/core/lightweight_test.hpp>
|
|
#include <boost/config.hpp>
|
|
#include <boost/config/workaround.hpp>
|
|
|
|
#if defined(BOOST_MSVC) && BOOST_MSVC < 1920
|
|
# pragma warning(disable: 4307) // integral constant overflow
|
|
#endif
|
|
|
|
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
|
|
|
// gcc-5 has issues with these constexpr tests:
|
|
// libs/hash2/test/sha3_cx.cpp:100:132: internal compiler error: in fold_binary_loc, at fold-const.c:9925
|
|
// TEST_EQ( test<sha3_256>( 0, str1 ), digest_from_hex( "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532" ) );
|
|
#if defined(BOOST_NO_CXX14_CONSTEXPR) || BOOST_WORKAROUND(BOOST_GCC, < 60000)
|
|
# 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
|
|
|
|
template<class H, std::size_t N> BOOST_CXX14_CONSTEXPR typename H::result_type test( std::uint64_t seed, char const (&str)[ N ] )
|
|
{
|
|
H h( seed );
|
|
|
|
std::size_t const M = N - 1; // strip off null-terminator
|
|
|
|
// only update( unsigned char const*, std::size_t ) is constexpr so we memcpy here to emulate bit_cast
|
|
unsigned char buf[M] = {};
|
|
for( unsigned i = 0; i < M; ++i ){ buf[i] = str[i]; }
|
|
|
|
h.update( buf, M / 3 );
|
|
h.update( buf + M / 3, M - M / 3 );
|
|
|
|
return h.result();
|
|
}
|
|
|
|
template<class H, std::size_t N> BOOST_CXX14_CONSTEXPR typename H::result_type test( std::uint64_t seed, unsigned char const (&buf)[ N ] )
|
|
{
|
|
H h( seed );
|
|
|
|
h.update( buf, N / 3 );
|
|
h.update( buf + N / 3, N - N / 3 );
|
|
|
|
return h.result();
|
|
}
|
|
|
|
|
|
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<std::size_t N, std::size_t M = ( N - 1 ) / 2>
|
|
BOOST_CXX14_CONSTEXPR boost::hash2::digest<M> digest_from_hex( char const (&str)[ N ] )
|
|
{
|
|
boost::hash2::digest<M> 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;
|
|
}
|
|
|
|
template<class H, std::size_t N> BOOST_CXX14_CONSTEXPR typename H::result_type test_hex( std::uint64_t seed, char const (&str)[ N ] )
|
|
{
|
|
H h( seed );
|
|
|
|
std::size_t const M = ( N - 1 ) / 2;
|
|
|
|
unsigned char buf[M] = {};
|
|
for( unsigned i = 0; i < M; ++i ) {
|
|
auto c1 = to_byte( str[ 2 * i ] );
|
|
auto c2 = to_byte( str[ 2 * i + 1 ] );
|
|
buf[ i ] = ( c1 << 4 ) | c2;
|
|
}
|
|
|
|
h.update( buf, M / 3 );
|
|
h.update( buf + M / 3, M - M / 3 );
|
|
|
|
return h.result();
|
|
}
|
|
|
|
int main()
|
|
{
|
|
using namespace boost::hash2;
|
|
|
|
{
|
|
constexpr char const str1[] = "abc";
|
|
constexpr char const str2[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
|
constexpr char const str3[] = "\xbd";
|
|
constexpr char const str4[] = "\xc9\x8c\x8e\x55";
|
|
|
|
TEST_EQ( test<sha3_256>( 0, str1 ), digest_from_hex( "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532" ) );
|
|
TEST_EQ( test<sha3_256>( 0, str2 ), digest_from_hex( "41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376" ) );
|
|
TEST_EQ( test<sha3_256>( 0, str3 ), digest_from_hex( "b389fa0f45f21196cc2736e8de396497a2414be31e7a500a499918b8cf3257b2" ) );
|
|
TEST_EQ( test<sha3_256>( 0, str4 ), digest_from_hex( "faad1cd75a6947e88d210123959b29d8f0973b8d094582debe56742878f412f6" ) );
|
|
|
|
TEST_EQ( test<sha3_256>( 7, str1 ), digest_from_hex( "767b76e08c084e1ceb87f67e0ec20d02892a7842d91091dbc141d73bab54ee3b" ) );
|
|
TEST_EQ( test<sha3_256>( 7, str2 ), digest_from_hex( "6e62b382e3a6d91715f5c488e1318196d0bc4c875f8bc7229c717aa4fa31f5ae" ) );
|
|
TEST_EQ( test<sha3_256>( 7, str3 ), digest_from_hex( "c1c987cdc6fd9dff69ca8bbd39f8ff5afc412545f046232c4e97402d6c94a2bc" ) );
|
|
TEST_EQ( test<sha3_256>( 7, str4 ), digest_from_hex( "a0eb0aeef201cf512ac37017810b8aae4d6f71f40e376195dcbf2848cb743d01" ) );
|
|
}
|
|
|
|
return boost::report_errors();
|
|
}
|