我正在使用 SHA512 来获取一些消息的摘要。我想将摘要转换为 int(可能是 512 位的 int)。我写了代码,但我注意到我总是得到 0 作为答案。我用 python 和 c 编写代码,在 python 中更容易。
这是我在 C 中的代码:
#include <stdio.h>
#include <stdlib.h>
#include <OpenSSL/sha.h>
int main() {
unsigned char message[] = "hello world";
unsigned char[512] digest;
SHA512(message,strlen((char *) message),digest)
int integer_value = atoi((char *) digest);
printf("The integer value of the message is %d\n", integer_value);
return 0;
}
在 python 中要容易得多,而且工作正常!
import hashlib
import sys
Message =“hello world”
integer_value = int.from_bytes(hashlib.sha512(Message.encode()).digest(),sys.byteorder)
我是 C 的新手,我整天都在想解决它的想法,但是与 python 相比,C 有很多事情要做。我的目标只是从 C 中的摘要中获取 int 值,就像我在 python 中所做的那样。我也尝试使用 strtol 而不是 atoi,但仍然得到 0 作为答案。