fnv1.hHeader

This header file defines the fnv1 class template and related utilities for computing FNV-1 (Fowler-Noll-Vo) hash values. More...

Defines

TypeName
ANTISPY_FNV1
ANTISPY_FNV1_RT

Detailed Description

The fnv1.h file provides a comprehensive implementation of the FNV-1 hashing algorithm, which is a widely-used non-cryptographic hash function. This algorithm is known for its simplicity and speed, making it ideal for use in hash tables, checksums, and other scenarios requiring quick hash computations.

Key Features:

  • FNV-1 Hash Algorithm :

    • Implements the FNV-1 hash function, which combines multiplication by a prime number with XOR operations to produce a hash value.

    • Supports both compile-time and runtime hash computation, offering flexibility for various use cases.

  • Template-Based Design :

    • The fnv1 class template is type-generic, allowing it to hash data of different types, such as char , uint32_t , and uint64_t .

    • Specializations for compile-time and runtime hash calculations are provided through nested classes.

  • Secure Implementation :

    • The implementation includes structural randomization and other techniques to protect the computed hash values from tampering or inspection.

Usage Scenarios:

The FNV-1 hash function is often used in:

  • Hash Tables : To quickly generate hash codes for keys.

  • Data Integrity : To create checksums that verify the integrity of data.

  • Identifiers : To create unique, deterministic identifiers for objects.

Example Usage:

Compile-Time Hashing:

#include <antispy/libantispy.h>

int main() {
   constexpr char data[] = "Hello";
   constexpr libantispy::fnv1<char> hash_value(data);
   size_t hash = hash_value;  // The hash is computed at compile-time.
}

Runtime Hashing:

#include <antispy/libantispy.h>

int main() {
   const char* data = "Hello, World!";
   size_t length = strlen(data);
   libantispy::fnv1<char> hash_value(data, length);
   size_t hash = hash_value;  // The hash is computed at runtime.
}

This header file is designed to be included wherever FNV-1 hashing is required within the antispy SDK library, ensuring consistent and efficient hash computations across the project.

Defines

ANTISPY_FNV1

Macro to generate an FNV-1 hash for a given input.

Detailed Description

The ANTISPY_FNV1 macro simplifies the creation of an FNV-1 hash object by automatically determining the type of the input and passing it to the fnv1 class. This macro is designed to work with various types of input, including strings, arrays, and other types that can be hashed using the FNV-1 algorithm.

Parameters

  • t: The input for which the FNV-1 hash is to be generated. This input can be of any type that is compatible with the fnv1 class. The macro automatically deduces the type of the input and processes it accordingly.

The ANTISPY_FNV1 macro works by removing any const , reference, or array extents from the input type, ensuring that the correct base type is used for the hash computation. This type deduction and transformation are handled using std::remove_const , std::remove_reference , and std::remove_all_extents , ensuring that the input is properly prepared before being passed to the fnv1 class.

The fnv1 class then computes the FNV-1 hash of the input, which can be used in various security-related operations, such as data integrity checks, unique identifiers, or cryptographic applications.

The FNV-1 hash algorithm is a non-cryptographic hash function that is particularly fast and simple. It is widely used in applications where a quick and reasonably distributed hash value is required.

Example usage:

#include <antispy/libantispy.h>

int main() {
   constexpr auto hash_value = ANTISPY_FNV1("Hello, World!");
}

In this example, the ANTISPY_FNV1 macro is used to generate an FNV-1 hash of the string "Hello, World!". The macro automatically deduces the type of the input and computes the corresponding hash value.

  • fnv1 class for details on the FNV-1 hash computation.

  • std::remove_const , std::remove_reference , and std::remove_all_extents for type transformations.

ANTISPY_FNV1_RT

Macro to generate an FNV-1 hash for a given input at runtime.

Detailed Description

The ANTISPY_FNV1_RT macro is designed to create an FNV-1 hash object for a given input at runtime. It accepts an input and its size, making it suitable for hashing dynamically determined data.

Parameters

  • t: The input data for which the FNV-1 hash is to be generated. This can be a string, array, or any other type that can be hashed using the FNV-1 algorithm.

  • N: The size of the input data, typically determined at runtime.

The ANTISPY_FNV1_RT macro handles type deduction and processing, ensuring the correct base type of the input is passed to the fnv1 class for hash computation. This macro is particularly useful in situations where the input data and its size are not known until runtime.

The fnv1 class then computes the FNV-1 hash of the input based on the provided size, making this macro versatile for applications requiring runtime hashing.

The FNV-1 hash algorithm is a non-cryptographic hash function that is fast and straightforward, making it ideal for applications that need quick and fairly distributed hash values.

The FNV-1 hash function, while efficient and easy to implement, is not intended for cryptographic security. It is best used in scenarios where performance and simplicity are more important than cryptographic strength.

Example usage:

#include <antispy/libantispy.h>

int main() {
   std::string data = "Runtime Data";
   size_t length = data.size();
   auto hash_value = ANTISPY_FNV1_RT(data.c_str(), length);
}

In this example, the ANTISPY_FNV1_RT macro generates an FNV-1 hash for the string "Runtime Data" with the size determined at runtime. The macro automatically deduces the type and computes the appropriate hash.

  • fnv1 class for details on the FNV-1 hash computation.

  • ANTISPY_FNV1 macro for compile-time hashing.

  • std::remove_const , std::remove_reference , and std::remove_all_extents for type transformations.