fnv1Class
Class template for FNV-1 hash computation. More...
Public Functions
Type | Name |
---|---|
constexpr | fnv1 (typename id< U >::type z) |
constexpr | fnv1 (const T(&t)[N]) |
fnv1 (const T *t, size_t N) | |
constexpr | operator size_t () const |
Private Properties
Type | Name |
---|---|
const_value< size_t > | value_ |
Private Functions
Type | Name |
---|---|
constexpr | fnv1 (std::initializer_list< T > &t, std::index_sequence< Ns... >) |
Detailed Description
The fnv1
class template implements the FNV-1 (Fowler-Noll-Vo) hashing algorithm, which is a non-cryptographic hash function widely used for fast and effective hash computation. This class provides functionality to compute the FNV-1 hash value for various data types, typically for hashing strings or binary data.
Key Features:
Compile-Time and Runtime Hashing :
The class supports both compile-time and runtime hash computation.
For compile-time, the hash is calculated using constant expressions, providing the benefit of embedding the result directly into the compiled code.
For runtime, the hash is computed dynamically, suitable for cases where the input data is not known at compile time.
Type-Generic :
The class is templated on
T
, allowing it to compute hash values for different types, such aschar
,uint8_t
,uint32_t
, anduint64_t
.This flexibility makes it suitable for hashing a wide range of data types and lengths.
FNV-1 Algorithm :
Implements the standard FNV-1 algorithm, which is known for its simplicity and good distribution properties.
The algorithm works by starting with an offset basis and then iteratively multiplying by a prime number while XORing with the input data.
Efficient Hashing :
Designed to provide a fast and efficient way to compute hash values, particularly useful in hash tables, checksums, and other situations where a quick and reliable hash is required.
The hash value generated is deterministic, meaning the same input will always produce the same hash value.
Usage:
The fnv1
class can be used to compute the hash of data as follows:
int
Example:
int
Template Parameters
T
: The type used for the hash value, typicallyuint32_t
oruint64_t
.
Public Functions
constexpr fnv1 (typename id< U >::type z)
Constructs an fnv1
object using a compile-time constant initializer list.
Definition
constexpr libantispy::fnv1< T >::fnv1
Detailed Description
This constructor is designed to create an fnv1
object by computing the FNV-1 hash for a compile-time constant input, provided as an initializer list. The hash is calculated at compile-time, making this constructor particularly useful for embedding precomputed hash values directly into the compiled code, thereby improving runtime efficiency.
Template Parameters
N
: The size of the input initializer list, representing the number of elements in the list.U
: The type of the initializer list, defaulting tostd::initializer_list<T>
. This template parameter allows flexibility in the type of input data that can be passed, although typically it is used with lists of typeT
.
Parameters
z
: An initializer list containing the elements to be hashed. This list is processed to compute the FNV-1 hash. The type of this parameter is determined by theU
template parameter, which defaults tostd::initializer_list<T>
.
The constructor leverages the std::make_index_sequence<N>()
to create a sequence of indices that corresponds to the size of the initializer list. This index sequence is then used to expand and process each element in the initializer list to compute the FNV-1 hash value. The computation is performed in a constexpr context, allowing the result to be determined at compile-time, ensuring that the final hash value is embedded directly into the resulting binary.
Example usage:
int
In this example, the hash value for the string "Hello" is computed at compile-time and stored in hash_value
.
constexpr fnv1 (const T(&t)[N])
Constructs an fnv1
object using a compile-time constant input array.
Definition
constexpr libantispy::fnv1< T >::fnv1
Detailed Description
This constructor is designed to create an fnv1
object by calculating the FNV-1 hash for a compile-time constant array of type T
. The input array is processed at compile-time, and the resulting hash value is stored within the fnv1
object, allowing the hash to be embedded directly into the compiled binary.
Template Parameters
N
: The size of the input array, representing the number of elements in the array.
Parameters
t
: A reference to the compile-time constant array of typeT
whose elements are to be hashed. The array is passed by reference and its elements are used to compute the FNV-1 hash.
This constructor uses the input array t
to compute the FNV-1 hash value. The computation is performed by invoking a static run
method from the detail::fnv1<T, N>
struct, which processes the elements of the array and returns the computed hash. The result is then stored in the value_
member of the fnv1
object. This entire process occurs in a constexpr
context, allowing the hash calculation to be done at compile-time.
Example usage:
int
In this example, the hash value for the string "Hello" (represented as a char
array) is computed at compile-time and stored in hash_value
.
The use of a compile-time constant array allows the hash computation to be highly efficient, as the result is known at compile-time and can be embedded directly into the program's binary. This approach is particularly beneficial in performance-critical applications where runtime computation needs to be minimized.
fnv1 (const T *t, size_t N)
Constructs an fnv1
object using a runtime input string or array.
Definition
libantispy::fnv1< T >::fnv1
Detailed Description
This constructor allows the creation of an fnv1
object by calculating the FNV-1 hash for a given runtime input string or array of type T
. The input data is processed at runtime, and the resulting hash value is stored within the fnv1
object.
Parameters
t
: A pointer to the runtime input string or array of typeT
whose elements are to be hashed. The pointer should point to the first element of the string or array.N
: The size of the input string or array, representing the number of elements to be hashed. This value is typically the length of the string or the size of the array.
This constructor uses the input pointer t
and the size N
to compute the FNV-1 hash value at runtime. The computation is performed by invoking a static run
method from the detail::fnv1_rt<T>
struct, which processes the elements of the input data and returns the computed hash. The result is then stored in the value_
member of the fnv1
object.
Example usage:
int
In this example, the hash value for the string "Hello, world!" is computed at runtime using the fnv1
constructor and stored in hash_value
.
This constructor is useful when the input data is only known at runtime, such as when processing user input, data from files, or network messages. The runtime nature of this constructor allows it to handle dynamic data that cannot be processed at compile-time.
Ensure that the input pointer t
is valid and points to a memory region with at least N
elements. Undefined behavior may occur if the pointer is invalid or the size N
is incorrect.
constexpr operator size_t () const
Conversion operator to size_t
.
Definition
constexpr libantispy::fnv1< T >::operator size_t
Detailed Description
This conversion operator allows the fnv1
object to be implicitly converted to a size_t
, representing the computed FNV-1 hash value. This operator provides convenient access to the hash value stored within the fnv1
object, enabling it to be used directly in contexts where a size_t
hash value is required.
Returns
The computed FNV-1 hash value as a size_t
.
The conversion operator returns the value_
member of the fnv1
object, which stores the result of the FNV-1 hash calculation. By providing this operator, the fnv1
class allows for seamless integration with other components that expect a size_t
hash value, without the need for explicit type casting.
Example usage:
int
In this example, the fnv1
object hash_value
is implicitly converted to a size_t
when assigned to the variable hash
. This is made possible by the conversion operator, which extracts the stored hash value.
The use of this operator is particularly useful in hash-based data structures (e.g., hash tables) or when comparing hash values. It simplifies the interaction with the fnv1
class by removing the need for explicit method calls to retrieve the hash.
Private Properties
const_value< size_t > value_
Stores the computed FNV-1 hash value.
Definition
const_value<size_t>
Detailed Description
The value_
member is of type const_value<size_t>
, which securely stores the computed FNV-1 hash value. This member variable encapsulates the hash value, ensuring that it is immutable and protected against certain types of attacks, such as memory tampering or inspection.
The const_value
template class is designed to store constant values in a way that adds additional layers of security. By wrapping the hash value in a const_value<size_t>
, the value_
member benefits from structural randomization and obfuscation techniques. This makes it more difficult for attackers to predict or alter the stored value.
The FNV-1 hash value itself is a unique, fixed-size identifier that is computed based on the input data provided to the fnv1
class. Once calculated, the hash value is stored in value_
, ensuring that it remains constant and secure throughout the lifetime of the fnv1
object.
This design is particularly useful in security-sensitive applications, where the integrity and confidentiality of hash values are paramount. The value_
member not only holds the result of the hash computation but also safeguards it against potential security breaches.
Example usage:
int
In this example, the value_
member would store the FNV-1 hash of the string "Hello, World!", ensuring that the hash remains constant and protected.
The value_
member is initialized during the construction of the fnv1
object and is not intended to be modified afterward. This immutability is crucial for maintaining the integrity of the hash value in secure environments.
Private Functions
constexpr fnv1 (std::initializer_list< T > &t, std::index_sequence< Ns... >)
Constructs an fnv1
object with an initializer list and index sequence.
Definition
constexpr libantispy::fnv1< T >::fnv1
Detailed Description
This constructor initializes the fnv1
object by computing the FNV-1 hash value based on the input data provided in the form of an initializer list and an index sequence. It is designed to work with compile-time constants, allowing the hash computation to be performed at compile-time.
Template Parameters
Ns
: The index sequence that represents the indices of the elements in the initializer list.
Parameters
t
: The initializer list containing the input data for which the FNV-1 hash is to be calculated.
This constructor leverages an index sequence to access elements in the initializer list t
. The index_sequence<Ns...>
is used to unpack the indices at compile-time, which are then used to pass the corresponding elements from the initializer list to the detail::fnv1::run
function.
The run
function, defined in the detail::fnv1
namespace, performs the FNV-1 hash computation over the provided data, and the resulting hash value is stored in the value_
member of the fnv1
object.
This constructor is constexpr
, meaning that the hash computation can be evaluated at compile-time if all inputs are known at compile-time, enabling efficient and secure hash value storage in embedded systems or scenarios with strict performance requirements.
Example usage:
int
In this example, the constructor computes the FNV-1 hash of the characters in the data
initializer list, with the std::make_index_sequence<5>()
providing the indices for accessing each character.
This constructor is primarily intended for use cases where the input data is known at compile-time, allowing the FNV-1 hash to be precomputed and embedded directly into the compiled binary. It is useful in contexts where immutable, fixed-size data needs to be hashed, such as in secure hash storage, compile-time checks, or constant expressions.