sdi_toolBox
hash.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026 - SD-Innovation S.A.S. - FRANCE
3*/
4
5/*
6ver: 2.x.x - build: 2026-04-28
7*/
8
9/*
10The zlib License Copyright (c) 2026 SD-Innovation S.A.S. This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
11*/
12
13#pragma once
14
15#include <cstdint>
16#include <span>
17#include <string_view>
18
20{
21//--------------------------------------------------------------
31constexpr std::uint32_t fnv1a(std::span<const std::byte> data) noexcept;
32
41constexpr std::uint32_t fnv1a(std::string_view sv) noexcept;
42
53template<typename T>
54 requires std::is_trivially_copyable_v<T>
55constexpr std::uint32_t fnv1a(const T &value) noexcept;
56
66constexpr std::uint64_t fnv1a_64(std::span<const std::byte> data) noexcept;
67
76constexpr std::uint64_t fnv1a_64(std::string_view sv) noexcept;
77
88template<typename T>
89 requires std::is_trivially_copyable_v<T>
90constexpr std::uint64_t fnv1a_64(const T &value) noexcept;
91//--------------------------------------------------------------
92
93//--------------------------------------------------------------
94/* Compute the FNV-1a 32-bit hash for a span of bytes */
95inline constexpr std::uint32_t fnv1a(const std::span<const std::byte> data) noexcept
96{
97 constexpr std::uint32_t FNV_OFFSET_BASIS = 2166136261u;
98 constexpr std::uint32_t FNV_PRIME = 16777619u;
99
100 std::uint32_t hash = FNV_OFFSET_BASIS;
101 for (const auto b : data)
102 {
103 hash ^= static_cast<std::uint32_t>(std::to_integer<std::uint8_t>(b));
104 hash *= FNV_PRIME;
105 }
106 return hash;
107}
108//--------------------------------------------------------------
109/* Compute the FNV-1a 32-bit hash for a std::string_view */
110inline constexpr std::uint32_t fnv1a(const std::string_view sv) noexcept
111{
112 return fnv1a({ reinterpret_cast<const std::byte *>(sv.data()), sv.size() });
113}
114//--------------------------------------------------------------
115/* Compute the FNV-1a 32-bit hash for a trivially copyable POD object */
116template<typename T>
117 requires std::is_trivially_copyable_v<T>
118inline constexpr std::uint32_t fnv1a(const T &value) noexcept
119{
120 return fnv1a({ reinterpret_cast<const std::byte *>(std::addressof(value)), sizeof(T) });
121}
122//--------------------------------------------------------------
123/* Compute the FNV-1a 64-bit hash for a span of bytes */
124inline constexpr std::uint64_t fnv1a_64(const std::span<const std::byte> data) noexcept
125{
126 constexpr std::uint64_t FNV_OFFSET_BASIS = 14695981039346656037ull;
127 constexpr std::uint64_t FNV_PRIME = 1099511628211ull;
128
129 std::uint64_t hash = FNV_OFFSET_BASIS;
130 for (const auto b : data)
131 {
132 hash ^= static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(b));
133 hash *= FNV_PRIME;
134 }
135 return hash;
136}
137//--------------------------------------------------------------
138/* Compute the FNV-1a 64-bit hash for a std::string_view */
139inline constexpr std::uint64_t fnv1a_64(const std::string_view sv) noexcept
140{
141 return fnv1a_64({ reinterpret_cast<const std::byte *>(sv.data()), sv.size() });
142}
143//--------------------------------------------------------------
144/* Compute the FNV-1a 64-bit hash for a trivially copyable POD object */
145template<typename T>
146 requires std::is_trivially_copyable_v<T>
147inline constexpr std::uint64_t fnv1a_64(const T &value) noexcept
148{
149 return fnv1a_64(std::span<const std::byte>{ reinterpret_cast<const std::byte *>(std::addressof(value)), sizeof(T) });
150}
151//--------------------------------------------------------------
152} // namespace sdi_toolBox::common::utils::hash
Lightweight hashing utilities (FNV-1a implementations).
Definition hash.h:20
constexpr std::uint64_t fnv1a_64(std::span< const std::byte > data) noexcept
Compute the FNV-1a 64-bit hash for a span of bytes.
Definition hash.h:124
constexpr std::uint32_t fnv1a(std::span< const std::byte > data) noexcept
Compute the FNV-1a 32-bit hash for a span of bytes.
Definition hash.h:95