欢迎参观我的个人博客:无敌牛 – 技术/著作/典籍/分享等
简介
分享一个快速计算HASH的开源C语言库–xxhash
源代码地址:GitHub - Cyan4973/xxHash: Extremely fast non-cryptographic hash algorithm
xxHash 是一种极快的哈希算法,它的限制在于 RAM 的速度。 代码具有高度可移植性,并在所有平台上生成相同的哈希值(小端/大端)。 该库包括以下算法:
- XXH32 :使用 32bits 计算,生成 32bits 哈希
- XXH64 :使用 64bits 计算,生成 64bits 哈希
- XXH3 (从
v0.8.0
开始):使用矢量化算术生成 64bits 或 128bits 哈希。 128 位称为 XXH128。
编译安装
下载:git clone https://github.com/Cyan4973/xxHash.git
编译安装:make -j 6 && make install
示例
给出的示例是提供了 XXH32_hash_t
和 XXH64_hash_t
类型的hash计算以及 XXH3
中 XXH64_hash_t
和 XXH128_hash_t
类型的 hash 计算。
读取一个给定的文件,然后把文件内容逐步计算出对应的 hash 值。
测试代码txxh.c
// gcc -Wall -o test txxh.c -lxxhash
// ./test /etc/service
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <xxhash.h>
XXH32_hash_t xxh32_compute_file_hash(const char *file_path) {
FILE *file = fopen(file_path, "rb");
if (file == NULL) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
// 创建 state
XXH32_state_t * state = XXH32_createState() ;
// 重置 state
XXH32_reset (state, 0) ;
char buffer[4096];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// 读文件逐步计算
XXH32_update(state, buffer, bytes_read);
}
fclose(file);
// 获取hash数字
XXH32_hash_t xxh32 = XXH32_digest(state);
// 释放 state
XXH32_freeState(state) ;
return xxh32 ;
}
XXH64_hash_t xxh64_compute_file_hash(const char *file_path) {
FILE *file = fopen(file_path, "rb");
if (file == NULL) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
// 创建 state
XXH64_state_t * state = XXH64_createState() ;
// 重置 state
XXH64_reset (state, 0) ;
char buffer[4096];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// 读文件逐步计算
XXH64_update(state, buffer, bytes_read);
}
fclose(file);
// 获取hash数字
XXH64_hash_t xxh64 = XXH64_digest(state);
// 释放 state
XXH64_freeState(state) ;
return xxh64 ;
}
XXH64_hash_t xxh3_64_compute_file_hash(const char *file_path) {
FILE *file = fopen(file_path, "rb");
if (file == NULL) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
// 创建 state
XXH3_state_t * state = XXH3_createState() ;
// 重置 state
XXH3_64bits_reset(state) ;
char buffer[4096];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// 读文件逐步计算
XXH3_64bits_update(state, buffer, bytes_read);
}
fclose(file);
// 获取hash数字
XXH64_hash_t xxh64 = XXH3_64bits_digest(state) ;
// 释放 state
XXH3_freeState(state) ;
return xxh64 ;
}
XXH128_hash_t xxh3_128_compute_file_hash(const char *file_path) {
FILE *file = fopen(file_path, "rb");
if (file == NULL) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
// 创建 state
XXH3_state_t * state = XXH3_createState() ;
// 重置 state
XXH3_128bits_reset(state) ;
char buffer[4096];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// 读文件逐步计算
XXH3_128bits_update(state, buffer, bytes_read);
}
fclose(file);
// 获取hash数字
XXH128_hash_t xxh128 = XXH3_128bits_digest(state) ;
// 释放 state
XXH3_freeState(state) ;
return xxh128 ;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <file_path>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char *file_path = argv[1];
XXH32_hash_t hash32 = xxh32_compute_file_hash(file_path) ;
printf("32-bit hash of file '%s': %X\n", file_path, hash32);
XXH64_hash_t hash64 = xxh64_compute_file_hash(file_path);
printf("64-bit hash of file '%s': %lX\n", file_path, hash64);
XXH64_hash_t hash64_2 = xxh3_64_compute_file_hash(file_path) ;
printf("h3-64-bit hash of file '%s': %lX\n", file_path, hash64_2) ;
XXH128_hash_t hash128 = xxh3_128_compute_file_hash(file_path);
printf("h3-128-bit hash of file '%s': %lX %lX\n", file_path, hash128.high64, hash128.low64);
return 0 ;
}
编译测试