添加哈希提取器代码

This commit is contained in:
zsyg
2025-08-01 11:11:21 +08:00
committed by GitHub
parent 8805b12223
commit 1a31b70fbb
9 changed files with 595 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
#ifndef HASH_CALCULATOR_H
#define HASH_CALCULATOR_H
#include <stddef.h>
typedef enum {
HASH_MD5,
HASH_SHA256,
HASH_SHA512
} HashAlgorithm;
// 各算法计算函数
int calculate_md5(const char* filename, char* output);
int calculate_sha256(const char* filename, char* output);
int calculate_sha512(const char* filename, char* output);
/**
* 计算文件的哈希值
* @param filename 文件路径
* @param algorithm 哈希算法
* @param output 输出缓冲区(必须足够大)
* @return 成功返回0失败返回-1
*/
static inline int calculate_file_hash(const char* filename, HashAlgorithm algorithm, char* output) {
switch (algorithm) {
case HASH_MD5:
return calculate_md5(filename, output);
case HASH_SHA256:
return calculate_sha256(filename, output);
case HASH_SHA512:
return calculate_sha512(filename, output);
default:
return -1;
}
}
#endif // HASH_CALCULATOR_H

View File

@@ -0,0 +1,21 @@
#ifndef STRING_UTIL_H
#define STRING_UTIL_H
#include <stddef.h>
// 快速分配字符串内存
char* str_alloc(size_t size);
// 快速释放字符串内存
void str_free(char* str);
// 快速字符串复制
char* str_copy(const char* src);
// 快速字符串连接
char* str_concat(const char* str1, const char* str2);
// 二进制转十六进制字符串
char* bin_to_hex(const unsigned char* data, size_t len);
#endif // STRING_UTIL_H