fnv1aClass
Class template for computing FNV-1a hash values. More...
Public Functions
Type | Name |
---|---|
constexpr | fnv1a (typename id< U >::type z) |
constexpr | fnv1a (const T(&t)[N]) |
fnv1a (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 | fnv1a (std::initializer_list< T > &t, std::index_sequence< Ns... >) |
Detailed Description
The fnv1a
class implements the FNV-1a (Fowler-Noll-Vo) hashing algorithm, a non-cryptographic hash function known for its simplicity, speed, and effectiveness in producing uniformly distributed hash values. The FNV-1a variant is widely used in hash tables, data integrity checks, and other scenarios where fast and reliable hashing is required.
The class supports both compile-time and runtime hash computation, making it versatile for use in a wide range of applications. When the input data is known at compile time, the hash can be computed during compilation, resulting in a constant expression. For data that is only available at runtime, the hash is computed dynamically.
Template Parameters
T
: The type of the elements to be hashed. Typically, this will bechar
orunsigned char
, but the class is generic and can work with any type that is compatible with the FNV-1a algorithm.
Example Usage:
int
The class template fnv1a
provides a convenient interface for both scenarios. The computed hash value can be retrieved by simply converting the object to size_t
.
Public Functions
constexpr fnv1a (typename id< U >::type z)
Constructs an fnv1a
object with a compile-time constant input.
Definition
constexpr libantispy::fnv1a< T >::fnv1a
Detailed Description
This constructor allows the creation of an fnv1a
object using a compile-time constant input string or array. The input is provided as an std::initializer_list
or a type that can be used in the same way. This constructor is particularly useful for computing the FNV-1a hash at compile time, allowing the result to be used as a constant expression.
The constructor internally creates an index sequence corresponding to the size of the input and delegates the actual hash computation to another constructor that uses this index sequence to process each element.
Template Parameters
N
: The length of the input string or array.U
: The type of the initializer list, defaulted tostd::initializer_list<T>
.
Parameters
z
: The initializer list containing the input data to be hashed.
Example Usage:
int
In this example, the input string "example" is passed to the constructor, and the FNV-1a hash is computed at compile time.
constexpr fnv1a (const T(&t)[N])
Constructs an fnv1a
object with a compile-time constant input array.
Definition
constexpr libantispy::fnv1a< T >::fnv1a
Detailed Description
This constructor allows the creation of an fnv1a
object using a compile-time constant input array. The input array is processed during compilation, enabling the computation of the FNV-1a hash value as a constant expression. This is particularly useful for scenarios where a hash value needs to be computed once and reused, without runtime overhead.
The constructor initializes the fnv1a
object by invoking the detail::fnv1a::run
function, which computes the hash value based on the input array.
Template Parameters
N
: The size of the input array.
Parameters
t
: The input array whose FNV-1a hash is to be computed.
Example Usage:
int
In this example, the input array "example" is passed to the constructor, and the FNV-1a hash is computed at compile time.
fnv1a (const T *t, size_t N)
Constructs an fnv1a
object with a runtime input string or array.
Definition
libantispy::fnv1a< T >::fnv1a
Detailed Description
This constructor computes the FNV-1a hash for a given input string or array at runtime. The input is processed during the program's execution, allowing for dynamic hashing of data whose size or content is only known at runtime.
The constructor initializes the fnv1a
object by invoking the detail::fnv1a_rt::run
function, which computes the hash value based on the provided input data.
Parameters
t
: A pointer to the input string or array for which the FNV-1a hash is to be computed.N
: The length of the input string or array.
Example Usage:
int
In this example, the input string "example" is passed to the constructor, and the FNV-1a hash is computed at runtime based on the string's content and length.
constexpr operator size_t () const
Conversion operator to size_t
.
Definition
constexpr libantispy::fnv1a< T >::operator size_t
Detailed Description
This conversion operator allows an fnv1a
object to be implicitly converted to a size_t
, which represents the computed FNV-1a hash value. This operator provides a convenient way to retrieve the hash value stored within the fnv1a
object.
Returns
The computed FNV-1a hash value as a size_t
.
Example Usage:
int
In this example, the fnv1a
object is implicitly converted to a size_t
to retrieve the computed hash value.
Private Properties
const_value< size_t > value_
Stores the computed FNV-1a hash value.
Definition
const_value<size_t>
Detailed Description
The value_
member is a constant value of type size_t
that holds the result of the FNV-1a hash computation. This value is generated during the construction of the fnv1a
object and remains immutable throughout the lifetime of the object. It is stored using the const_value<size_t>
type to ensure that it is securely managed and protected against unauthorized modifications or tampering.
The const_value
type provides additional security features, including structure layout randomization and other mechanisms, to enhance the robustness of the stored hash value.
Example Usage:
int
In this example, the value_
member holds the hash value computed from the string "example". The value is securely stored and can be accessed via the conversion operator to size_t
.
Private Functions
constexpr fnv1a (std::initializer_list< T > &t, std::index_sequence< Ns... >)
Constructs an fnv1a
object from an initializer list with an index sequence.
Definition
constexpr libantispy::fnv1a< T >::fnv1a
Detailed Description
This constructor initializes an fnv1a
object using a provided initializer list t
and an associated index sequence Ns...
. It computes the FNV-1a hash value for the elements in the initializer list and stores it in the value_
member.
Template Parameters
Ns
: A parameter pack representing the index sequence, which corresponds to the positions of the elements in the initializer list.
Parameters
t
: The initializer list containing the input data to be hashed.
Example Usage:
int
In this example, the fnv1a
object is constructed using a list of characters, and an index sequence is used to compute the FNV-1a hash value.