追加
-L/opt/homebrew/lib -lcrypto
到您的命令行。
此外,你的malloc和snprintf看起来很奇怪。整个+1和+7看起来容易出错
这样会更好:
char header[100] = {0}; // 100 is big enough for the entire "blob 12345" thing regardless of how big the file is
SHA1_CTX ctx = {0};
SHA1_Init(&ctx);
sprintf(header, "blob %lu", file_size);
SHA1_Update(&ctx, header, strlen(header));
SHA1_Update(&ctx, buffer, file_size);
SHA1_Final(hash, &ctx);
然后,如果你真的想变得迂腐,请确保在非Unix上以二进制文件的形式打开文件:
#ifdef _WIN32
char* flags = "rb";
#else
char* flags = "r";
#endif
FILE *src = fopen(file_name, flags);