日常工作开发中遇到的问题,将每一个问题写成一个通用函数,方便后续开发复用
#include <openssl/md5.h>
char hash_str[33] = {0};
int status = file_md5(",hash_str);int file_md5(const char *file_name,uint8_t *mds)
{if(file_name == NULL ){LOG_ERROR(c,"Error:file_name is null");return -2; }if(mds == NULL){LOG_ERROR(c,"Error:file_name is null");return -2; }FILE *fp = fopen(file_name, "rb");if (!fp) {LOG_ERROR(c,"Error: Failed to open file");return -1;}uint8_t buf[BUFFER_SIZE];uint8_t md[16];MD5_CTX ctx;MD5_Init(&ctx);size_t n;while ((n = fread(buf, 1, BUFFER_SIZE, fp)) > 0) {MD5_Update(&ctx, buf, n);}MD5_Final(md, &ctx);fclose(fp);// for (int i = 0; i < 16; i++) {// printf("%02x", md[i]);// }// printf("n");size_t i, pos = 0;for (int i = 0; i < 16 && pos < 33 - 1; i++) {pos += sprintf(mds + pos, "%02x", md[i]);}if (pos < 33) {mds[pos] = '