Search code examples
c++c++17hexsha256

Invalid hex string output for hashed (sha256) string (missing zeros)


I was writing a function to hash a string and get the result in a hex format. The output I get looks almost identical to expected, but it is shorter due to missing zeros:

64: ae6a9df8bdf4545392e6b1354252af8546282b49033a9118b12e9511892197c6
64: ae6a9df8bdf4545392e6b1354252af8546282b4933a9118b12e9511892197c6

Here's my code:


#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

I don't want to use std::stringstream since I don't need dynamic allocations.
I based my code on this answer: https://stackoverflow.com/a/72132640/9363996

What am I doing wrong here?


Solution

  • For the second solution (which was commented out) using hex table, I figured the error. I was using invalid iterator for hex[].

    The correct version:

        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