gpt4 book ai didi

c - strcmp() unsigned char 到文件中的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:34 24 4
gpt4 key购买 nike

我需要一种方法可以将此散列函数的输出与驻留在单独文件中的字符串进行比较,但输出通常定义为 printf("%02x", c[i]) ; (注释所在的位置)。这种格式不适用于比较,我不确定如何从根本上调整这种格式。

            FILE *ftest=fopen(hashInBuf, "rb");
SHA512_Init (&mdContext);
while ((bytes = fread (data, 1, 1024, ftest)) != 0)
SHA512_Update (&mdContext, data, bytes);
SHA512_Final (c,&mdContext);
if(access(hashOutBuf, F_OK) != -1){
for(i = 0; i < SHA512_DIGEST_LENGTH; i++){
//METHOD TO COMPARE WITH TO EXISTING HASH FILE (stored in seperate folder)
}
}

文件中的字符串是此哈希函数的输出(使用 fprintf(*file, "%02x", c[i]); 存储)- 这位于注释所在的位置多于。先感谢您。

最佳答案

将外部字符串的每对字符转换为整数。

const char *external_string = "2435CD76F...";
for(i = 0; i < SHA512_DIGEST_LENGTH; i++) {
// v------------------------------------------------v Compound literal (C99)
if (strtol((char [3]){external_string[0], external_string[1]}, 0, 16) != data[i]) {
return Different;
}
external_string += 2;
}
return Same;

确保 dataunsigned char 或使用 .... != (unsigned char) data[i]

关于c - strcmp() unsigned char 到文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55611365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com