我的代码应该从字符串内容中计算出SHA1哈希,然后将其打印为HEX值。
主要功能:
#include <openssl/sha.h>
int main(){
unsigned char str[] = "A";
unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
SHA1(str, sizeof(str), hash);
printHash(hash);
return 0;
}
Printhash功能:
void printHash(unsigned char *hash){
if(hash == NULL){
printf("HASH NULLn");
return;
}
for (int i = 0; i < SHA_DIGEST_LENGTH; i++){
printf("%02Xn", hash[i]);
}
}
字符串" a"的输出是:
EF420ABFDDBDA7B9EE665D85EF62E4A437554003
但是,当我尝试这样的网站时:http://www.sha1-online.com/,我得到了这个结果:
6dcd4ce23d88e2ee9568ba546c007c63d9131c1b
为什么有区别?
谢谢您的帮助。
我的愚蠢错误,我更改了这一行:
SHA1(str, sizeof(str), hash);
to
SHA1(str, sizeof(str)-1, hash);
现在一切都起作用,谢谢