如何哈希两次字符串 C openssl
How to hash twice a string C openssl
我的目标是用 sha256 对一个字符串进行两次哈希处理,但我没有得到很好的结果
这是代码:
void double_hash(char *string, char output[65]) {
unsigned char out[32];
char hash[65];
unsigned char scnd_hash[65];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(out, &sha256);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(hash + (i * 2), "%02x", out[i]);
}
SHA256_CTX sha256_2;
SHA256_Init(&sha256_2);
SHA256_Update(&sha256_2, hash, strlen(hash));
SHA256_Final(scnd_hash, &sha256_2);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(output + (i * 2), "%02x", scnd_hash[i]);
}
}
对于字符串“hello”,它的作用是:
hello
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
d7914fe546b684688bb95f4f888a92dfc680603a75f23eb823658031fff766d9
但我想要:
hello
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50
我试过这段代码,但没有产生好的结果:
#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>
void double_hash(char *string, char output[65]) {
unsigned char out[32];
unsigned char scnd_hash[65];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(out, &sha256);
SHA256_CTX sha256_2;
SHA256_Init(&sha256_2);
SHA256_Update(&sha256_2, out, strlen((char *)out));
SHA256_Final(scnd_hash, &sha256_2);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(output + (i * 2), "%02x", scnd_hash[i]);
}
}
int main() {
char outp[65];
char *hello = "hello";
double_hash(hello, outp);
printf("%s", outp);
}
它产生
852d59ce6fb3099c9d563870d298f0108eff079e1dde63e66c68d2e0b40353c1
谢谢
要以 9595c9df9007...
结束,对 32 字节二进制数据执行第二次哈希,而不是对 字符串。
// SHA256_Update(&sha256_2, hash, strlen(hash));
SHA256_Update(&sha256_2, out, 32);
参考:SHA256
我的目标是用 sha256 对一个字符串进行两次哈希处理,但我没有得到很好的结果 这是代码:
void double_hash(char *string, char output[65]) {
unsigned char out[32];
char hash[65];
unsigned char scnd_hash[65];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(out, &sha256);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(hash + (i * 2), "%02x", out[i]);
}
SHA256_CTX sha256_2;
SHA256_Init(&sha256_2);
SHA256_Update(&sha256_2, hash, strlen(hash));
SHA256_Final(scnd_hash, &sha256_2);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(output + (i * 2), "%02x", scnd_hash[i]);
}
}
对于字符串“hello”,它的作用是:
hello
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
d7914fe546b684688bb95f4f888a92dfc680603a75f23eb823658031fff766d9
但我想要:
hello
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50
我试过这段代码,但没有产生好的结果:
#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>
void double_hash(char *string, char output[65]) {
unsigned char out[32];
unsigned char scnd_hash[65];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(out, &sha256);
SHA256_CTX sha256_2;
SHA256_Init(&sha256_2);
SHA256_Update(&sha256_2, out, strlen((char *)out));
SHA256_Final(scnd_hash, &sha256_2);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(output + (i * 2), "%02x", scnd_hash[i]);
}
}
int main() {
char outp[65];
char *hello = "hello";
double_hash(hello, outp);
printf("%s", outp);
}
它产生
852d59ce6fb3099c9d563870d298f0108eff079e1dde63e66c68d2e0b40353c1
谢谢
要以 9595c9df9007...
结束,对 32 字节二进制数据执行第二次哈希,而不是对 字符串。
// SHA256_Update(&sha256_2, hash, strlen(hash));
SHA256_Update(&sha256_2, out, 32);
参考:SHA256