我一直在努力從openssl/sha.h獲取sha1()函數,但是我得到了隨機輸出和一些警告。我已經閱讀了很多內容,並嘗試了一些示例代碼,但是我收到了所有警告,並且顯示不正確。C中的SHA1錯誤實現
這裏是代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <openssl/sha.h>
int main()
{
const unsigned char data[] = "Hello, World";
unsigned long length = sizeof(data);
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(data, length, hash);
printf("%02x \n", hash);
return 0;
}
下面是我收到警告:
sha.c: In function ‘main’:
sha.c:12: warning: ‘SHA1’ is deprecated (declared at /usr/include/openssl/sha.h:124)
sha.c:13: warning: format ‘%02x’ expects type ‘unsigned int’, but argument 2 has type ‘unsigned char *’
sha.c:13: warning: format ‘%02x’ expects type ‘unsigned int’, but argument 2 has type ‘unsigned char *’
當我運行它,我得到的輸出: 62652b34
任何幫助將是大!
如果比較的結果一些試驗檯的數據,確保長度是正確的。在大多數情況下,「Hello,World」的長度應該是12,但在這裏我認爲它是13,因爲sizeof()也是ascii零字符。 –