-
Notifications
You must be signed in to change notification settings - Fork 0
Add FNV-1 and FNV-1a hash implementations with corresponding tests #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
683ab07
393eaaf
677b929
0da2d83
6ca95dd
862aa1a
894bf3b
b0a4aa8
ac43fe8
d7860d4
a990fec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // | ||
| // Created by Rene Windegger on 22/03/2026. | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <span> | ||
| #include <vector> | ||
|
|
||
| namespace hash23 { | ||
| class fnv_1 { | ||
| private: | ||
| std::vector<std::uint8_t> buffer_; | ||
|
|
||
| [[nodiscard]] static consteval std::size_t prime() { | ||
| if constexpr (sizeof(std::size_t) == 4) { | ||
| return 0x01000193uz; | ||
| } else { | ||
| return 0x00000100000001b3uz; | ||
| } | ||
| return 0uz; | ||
| } | ||
|
rwindegger marked this conversation as resolved.
Outdated
|
||
|
|
||
| [[nodiscard]] static consteval std::size_t offset_basis() { | ||
| if constexpr (sizeof(std::size_t) == 4) { | ||
| return 0x811c9dc5uz; | ||
| } else { | ||
| return 0xcbf29ce484222325uz; | ||
| } | ||
| return 0uz; | ||
|
rwindegger marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| template<typename T> | ||
| requires std::ranges::contiguous_range<T> | ||
| constexpr void update(T const &data) { | ||
| auto const start_offset = buffer_.size(); | ||
| buffer_.resize(start_offset + data.size()); | ||
| std::copy_n(data.data(), data.size(), buffer_.data() + start_offset); | ||
| } | ||
|
|
||
| [[nodiscard]] constexpr std::size_t finalize() const { | ||
| std::size_t hash = offset_basis(); | ||
|
|
||
| for (auto const &byte : buffer_) { | ||
| hash *= prime(); | ||
| hash ^= byte; | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
|
rwindegger marked this conversation as resolved.
|
||
| public: | ||
| template<typename T> | ||
| requires std::ranges::contiguous_range<T> | ||
| [[nodiscard]] static constexpr std::size_t calculate(T const &data) { | ||
| fnv_1 r; | ||
| r.update(data); | ||
| return r.finalize(); | ||
|
rwindegger marked this conversation as resolved.
|
||
| } | ||
|
|
||
| template<std::size_t N> | ||
| [[nodiscard]] static constexpr std::size_t calculate(char const (&str)[N]) { | ||
| auto const block = std::span(str, N - 1); | ||
| return calculate(block); | ||
| } | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||||||
| // | ||||||||||||||
| // Created by Rene Windegger on 22/03/2026. | ||||||||||||||
| // | ||||||||||||||
|
|
||||||||||||||
| #pragma once | ||||||||||||||
|
|
||||||||||||||
| #include <span> | ||||||||||||||
| #include <vector> | ||||||||||||||
|
||||||||||||||
| #include <vector> | |
| #include <vector> | |
| #include <cstdint> | |
| #include <cstddef> | |
| #include <algorithm> | |
| #include <ranges> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,6 @@ | |
|
|
||
| #pragma once | ||
|
|
||
| #include <hash23/fnv_1.h> | ||
| #include <hash23/fnv_1a.h> | ||
| #include <hash23/sha2_512.h> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ add_executable( | |
| hash23_tests | ||
| main.cpp | ||
| sha2_512_tests.cpp | ||
| fnv_1_tests.cpp | ||
| fnv_1a_tests.cpp | ||
| ) | ||
|
|
||
| if(MSVC) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // | ||
| // Created by Rene Windegger on 22/03/2026. | ||
| // | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <hash23/hash23.h> | ||
|
|
||
| namespace { | ||
| TEST(fnv_1, single_block_test) { | ||
| auto const actual = hash23::fnv_1::calculate("Hello, World!"); | ||
| constexpr std::size_t expected = 0x7b5ea4c513c14886uz; | ||
| EXPECT_EQ(expected, actual); | ||
|
rwindegger marked this conversation as resolved.
Outdated
|
||
| } | ||
|
rwindegger marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // | ||
| // Created by Rene Windegger on 22/03/2026. | ||
| // | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <hash23/hash23.h> | ||
|
|
||
| namespace { | ||
| TEST(fnv_1a, single_block_test) { | ||
| auto const actual = hash23::fnv_1a::calculate("Hello, World!"); | ||
| constexpr std::size_t expected = 0x6ef05bd7cc857c54uz; | ||
| EXPECT_EQ(expected, actual); | ||
|
rwindegger marked this conversation as resolved.
Outdated
|
||
| } | ||
|
rwindegger marked this conversation as resolved.
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.