1

我正在尝试生成要在区块链项目中使用的哈希值,在寻找加密库时,我偶然发现了整个 tomcrypt 并选择下载它,因为它很容易安装,但是现在我在创建哈希值时遇到了问题(顺便说一句,我我使用 SHA3_512 但该错误存在于所有其他 SHA 散列算法中)有时它输出正确的散列但被截断

照片示例 哈希截断示例

这是散列函数的代码

string hashSHA3_512(const std::string& input) {
//Initial
unsigned char* hashResult = new unsigned char[sha3_512_desc.hashsize];
//Initialize a state variable for the hash
hash_state md;
sha3_512_init(&md);
//Process the text - remember you can call process() multiple times
sha3_process(&md, (const unsigned char*) input.c_str(), input.size());
//Finish the hash calculation
sha3_done(&md, hashResult);
// Convert to string
string stringifiedHash(reinterpret_cast<char*>(hashResult));
// Return the result
return stringToHex(stringifiedHash);
}

这是 toHex 函数的代码,即使我已经检查过并且在调用此函数之前会弹出截断哈希问题

string stringToHex(const std::string& input)

{
    static const char hex_digits[] = "0123456789abcdef";

    std::string output;
    output.reserve(input.length() * 2);
    for (unsigned char c : input)
    {
        output.push_back(hex_digits[c >> 4]);
        output.push_back(hex_digits[c & 15]);
    }

return output;
}

如果有人知道这个图书馆或一般关于这个问题和可能的修复请向我解释,我从 3 天起就被困住了

更新 我发现程序在遇到 2 个十六进制连续零时会截断哈希,所以二进制中有 8 个零(或只是 2 个字节),但我仍然不明白为什么,如果你这样做,请让我和希望其他人使用同样的问题知道

4

0 回答 0