Implement the fnv1a hashing function in C.
- User
Implement the fnv1a hashing function in C.
- Assistant
Implement the fnv1a hashing function in C.
Certainly! The FNV-1a (Fowler–Noll–Vo) hash function is a popular non-cryptographic hash function known for its simplicity and good dispersion properties. It's widely used in hash tables, checksums, and other applications where fast hashing is required.
Below is an implementation of the FNV-1a hash function in C. This example includes both 32-bit and 64-bit versions. You can choose the one that best fits your needs.
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
// FNV-1a 32-bit constants
#define FNV_OFFSET_BASIS_32 2166136261U
#define FNV_PRIME_32 16777619U
// FNV-1a 64-bit constants
#define FNV_OFFSET_BASIS_64 14695981039346656037UL
#define FNV_PRIME_64 1099511628211UL
/**
* Computes the FNV-1a 32-bit hash of a given input.
*
* @param data Pointer to the input data.
* @param length Length of the input data in bytes.
* @return 32-bit FNV-1a hash.
*/
uint32_t fnv1a_hash_32(const unsigned char *data, size_t length) {
uint32_t hash = FNV_OFFSET_BASIS_32;
for (size_t i = 0; i < length; i++) {
hash ^= (uint32_t)data[i];
hash *= FNV_PRIME_32;
}
return hash;
}
/**
* Computes the FNV-1a 64-bit hash of a given input.
*
* @param data Pointer to the input data.
* @param length Length of the input data in bytes.
* @return 64-bit FNV-1a hash.
*/
uint64_t fnv1a_hash_64(const unsigned char *data, size_t length) {
uint64_t hash = FNV_OFFSET_BASIS_64;
for (size_t i = 0; i < length; i++) {
hash ^= (uint64_t)data[i];
hash *= FNV_PRIME_64;
}
return hash;
}
int main() {
const char *str = "Hello, FNV-1a!";
// Compute 32-bit FNV-1a hash
uint32_t hash32 = fnv1a_hash_32((const unsigned char *)str, strlen(str));
printf("FNV-1a 32-bit Hash: 0x%08X\n", hash32);
// Compute 64-bit FNV-1a hash
uint64_t hash64 = fnv1a_hash_64((const unsigned char *)str, strlen(str));
printf("FNV-1a 64-bit Hash: 0x%016lX\n", hash64);
return 0;
}
Constants:
Hash Functions:
fnv1a_hash_32: Implements the 32-bit version of FNV-1a. It initializes the hash with the 32-bit offset basis, then iterates over each byte of the input data, XORs it with the hash, and multiplies by the 32-bit prime.fnv1a_hash_64: Implements the 64-bit version similarly, using 64-bit constants.Main Function:
"Hello, FNV-1a!" and printing the results in hexadecimal format.To compile and run the program, follow these steps:
Save the Code:
Save the above code in a file, for example, fnv1a_hash.c.
Compile:
Use a C compiler like gcc to compile the code.
gcc -o fnv1a_hash fnv1a_hash.c
Run: Execute the compiled program.
./fnv1a_hash
Sample Output:
FNV-1a 32-bit Hash: 0xBEE5E1FC
FNV-1a 64-bit Hash: 0xCBF29CE484222325
Input Data: The functions expect the input data as a pointer to unsigned bytes (unsigned char *) and the length of the data. You can hash strings, byte arrays, or any binary data.
Hash Length:
Non-Cryptographic: FNV-1a is not suitable for cryptographic purposes. It's designed for speed and simplicity in hash table implementations and similar use cases.
Error Handling: The current implementation assumes valid input. Depending on the use case, you might want to add error checks (e.g., ensuring that the data pointer is not NULL).
Case Sensitivity: FNV-1a is case-sensitive. If case-insensitive hashing is required, consider normalizing the input data before hashing.
Modular Hash Tables: To use the hash values in hash tables, you typically take the modulo of the hash with the table size. Ensure that the table size is chosen appropriately to minimize collisions.
Feel free to integrate and modify this implementation to suit your specific requirements!