c++ 哈希(sha256)字符串的十六进制字符串输出无效(缺少零)

pkwftd7m  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(126)

我写了一个函数来散列一个字符串,并得到十六进制格式的结果。我得到的输出看起来几乎与预期相同,但由于缺少零,它更短:

64: ae6a9df8bdf4545392e6b1354252af8546282b49033a9118b12e9511892197c6
64: ae6a9df8bdf4545392e6b1354252af8546282b4933a9118b12e9511892197c6

下面是我的代码:

#include <openssl/evp.h>

#include <cstdint>
#include <array>
#include <string_view>
#include <charconv>

#include <iostream>

int main(){
    std::string oceanic = "oceanic 815";

    EVP_MD_CTX *context = EVP_MD_CTX_new(); 
    EVP_DigestInit(context, EVP_sha256());
    EVP_DigestUpdate(context, oceanic.data(), oceanic.size());

    std::array<uint8_t, EVP_MAX_MD_SIZE> hash{};
    unsigned int written = 0;
    EVP_DigestFinal(context, hash.data(), &written);

    std::cout << written << '/' << EVP_MAX_MD_SIZE << '\n';

    // from https://emn178.github.io/online-tools/sha256.html
    std::string_view expected = "ae6a9df8bdf4545392e6b1354252af8546282b49033a9118b12e9511892197c6";
    std::cout << expected.size() << ": " << expected << '\n';

    std::array<char, 64> hex{};
    for (size_t iHash = 0, iHex = 0; iHash < written; ++iHash)
    {
        std::to_chars(&hex[iHex], &hex[iHex + 2], hash[iHash], 16);
        iHex += 2;

        // This also produces invalid result
        // const char hexMap[] = "0123456789ABCDEF";
        // const char ch = hash[iHash];
        // hex[iHash] = hexMap[(ch & 0xF0) >> 4];
        // hex[iHash + 1] = hexMap[ch & 0xF];
        // iHash += 2;
    }

    std::cout << hex.size() << ": " << std::string_view(hex.data(), hex.size()) << '\n';
    return 0;
}

https://godbolt.org/z/hq9onW49z
我不想使用std::stringstream,因为我不需要动态分配。
我的代码基于以下答案:https://stackoverflow.com/a/72132640/9363996
我做错了什么?

kse8i1jr

kse8i1jr1#

对于使用十六进制表的第二个解决方案(已被注解掉),我发现了错误。我对hex[]使用了无效的迭代器。
正确版本:

std::array<char, 64> hex{};
    for (size_t iHash = 0, iHex = 0; iHash < written; ++iHash, iHex += 2)
    {
        constexpr const char hexMap[] = "0123456789abcdef";
        const char ch = hash[iHash];
        hex[iHex] = hexMap[(ch & 0xF0) >> 4];
        hex[iHex + 1] = hexMap[ch & 0x0F];
    }

https://godbolt.org/z/vYcn4Geos

相关问题