-3
我正在編寫一個程序來獲取某些文件的散列並對它們做些什麼。我試圖提取哈希等,但是當我編譯時,我得到一個錯誤。C SHA1()在編譯時打開文件並得到散列錯誤
#include <stdio.h>
#include <openssl/sha.h>
int main (int argc, char *argv[]) {
// para digitar em vez de //char str[50] = {0}; //scanf("Enter file name:%s", str); //scanf if (argc < 1) /* argc should be at least 2 for correct execution */
{
printf("falta o ficheiro para ter hash: %s filename", argv[0]);
}
else
{
// filename to open
FILE *file = fopen(argv[1], "r"); //char str[] = "teste"; //const char str[] = "Original String"; unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
SHA1(FILE, sizeof(FILE), hash);
printf("SHA1 of %s is %s\n", argv[1], hash);
/* fopen returns 0, the NULL pointer, on failure */
if (file == 0)
{
printf("Could not open file\n");
}
}
return 0; }
與編譯時收到此錯誤:GCC sob.c -o SHA -lcrypto
sob.c: In function ‘main’:
sob.c:24:7: error: expected expression before ‘FILE’
SHA1(FILE, sizeof(FILE), hash);
^
sob.c:24:7: error: too few arguments to function ‘SHA1’
In file included from sob.c:2:0:
/usr/include/openssl/sha.h:126:16: note: declared here
unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md);
^
錯誤消息告訴您,您正在使用不正確的(數量)參數調用該函數。你需要傳遞一個字符緩衝區作爲第一個參數;你正在嘗試傳遞一個類型名稱。即使您將'FILE'改爲'file',它也無法正常工作:無法將文件流傳遞給函數 - 您必須從文件中讀取數據並將數據傳遞給函數。我不確定第三個參數的目的;你需要閱讀函數的[手冊頁](https://www.openssl.org/docs/manmaster/crypto/sha.html)。 –
ty,我可以直接發送它。 –