libcruft-util/hash/siphash.hpp

59 lines
1.6 KiB
C++
Raw Normal View History

2018-01-19 11:31:12 +11:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/.
2018-01-19 11:31:12 +11:00
*
* Copyright 2016-2022 Danny Robson <danny@nerdcruft.net>
2018-01-19 11:31:12 +11:00
*/
#pragma once
2018-01-19 11:31:12 +11:00
#include "../view.hpp"
#include <cstdint>
#include <type_traits>
#include <span>
2018-01-19 11:31:12 +11:00
namespace cruft::hash {
constexpr int SIPHASH_DEFAULT_COMPRESSIONS = 2;
constexpr int SIPHASH_DEFAULT_FINALISATIONS = 4;
template <
int CompressionsV = SIPHASH_DEFAULT_COMPRESSIONS,
int FinalisationsV = SIPHASH_DEFAULT_FINALISATIONS,
int OutV = 8
>
2018-01-19 11:31:12 +11:00
class siphash {
public:
static_assert (OutV == 8 or OutV == 16);
2018-01-19 11:31:12 +11:00
static constexpr auto output_length = OutV;
2018-01-19 11:31:12 +11:00
static constexpr auto compression_rounds = CompressionsV;
static constexpr auto finalisation_rounds = FinalisationsV;
using digest_t = std::array<u08, output_length>;
siphash (std::array<u64, 2>) noexcept;
siphash (std::span<u08, 16>) noexcept;
2018-01-19 11:31:12 +11:00
digest_t operator() (cruft::view<u08 const*> data) const noexcept;
2018-01-19 11:31:12 +11:00
private:
std::array<u64, 2> m_key;
2018-01-19 11:31:12 +11:00
};
template <
int CompressionsV = SIPHASH_DEFAULT_COMPRESSIONS,
int FinalisationsV = SIPHASH_DEFAULT_FINALISATIONS
>
using siphash64 = siphash<CompressionsV, FinalisationsV, 8>;
template <
int CompressionsV = SIPHASH_DEFAULT_COMPRESSIONS,
int FinalisationsV = SIPHASH_DEFAULT_FINALISATIONS
>
using siphash128 = siphash<CompressionsV, FinalisationsV, 16>;
}