hash/siphash: add support by byte array keys

This commit is contained in:
Danny Robson 2022-07-21 14:22:47 +10:00
parent ce2f05ede2
commit b3f00b2f39
2 changed files with 41 additions and 6 deletions

View File

@ -43,6 +43,35 @@ round (uint64_t v[4])
}
///////////////////////////////////////////////////////////////////////////////
static
std::array<uint64_t, 2>
bytes_to_u64 (std::span<u08, 16> val)
{
u64 r0 =
u64 (val[0 + 0]) << u64 (0 * 8) |
u64 (val[0 + 1]) << u64 (1 * 8) |
u64 (val[0 + 2]) << u64 (2 * 8) |
u64 (val[0 + 3]) << u64 (3 * 8) |
u64 (val[0 + 4]) << u64 (4 * 8) |
u64 (val[0 + 5]) << u64 (5 * 8) |
u64 (val[0 + 6]) << u64 (6 * 8) |
u64 (val[0 + 7]) << u64 (7 * 8);
u64 r1 =
u64 (val[8 + 0]) << u64 (0 * 8) |
u64 (val[8 + 1]) << u64 (1 * 8) |
u64 (val[8 + 2]) << u64 (2 * 8) |
u64 (val[8 + 3]) << u64 (3 * 8) |
u64 (val[8 + 4]) << u64 (4 * 8) |
u64 (val[8 + 5]) << u64 (5 * 8) |
u64 (val[8 + 6]) << u64 (6 * 8) |
u64 (val[8 + 7]) << u64 (7 * 8);
return { r0, r1 };
}
///////////////////////////////////////////////////////////////////////////////
template <int C, int D>
siphash<C,D>::siphash (std::array<uint64_t,2> _key) noexcept:
@ -52,6 +81,13 @@ siphash<C,D>::siphash (std::array<uint64_t,2> _key) noexcept:
//-----------------------------------------------------------------------------
template <int C, int D>
siphash<C, D>::siphash (std::span<u08, 16> _key) noexcept
: siphash (bytes_to_u64 (_key))
{ ; }
///////////////////////////////////////////////////////////////////////////////
template <int C, int D>
typename siphash<C,D>::digest_t
siphash<C,D>::operator() (cruft::view<const uint8_t*> data) const noexcept
{

View File

@ -3,16 +3,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
* Copyright 2016-2022 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_HASH_SIPHASH_HPP
#define CRUFT_UTIL_HASH_SIPHASH_HPP
#pragma once
#include "../view.hpp"
#include <cstdint>
#include <type_traits>
#include <span>
namespace cruft::hash {
template <int CompressionsV = 2, int FinalisationsV = 4>
@ -24,12 +24,11 @@ namespace cruft::hash {
static constexpr auto finalisation_rounds = FinalisationsV;
siphash (std::array<uint64_t,2>) noexcept;
siphash (std::span<u08, 16>) noexcept;
digest_t operator() (cruft::view<const std::uint8_t*> data) const noexcept;
private:
std::array<uint64_t,2> m_key;
};
};
#endif
};