• caglararli@hotmail.com
  • 05386281520

OpenSSL: best way to get sha256 hash of two sha256 hashes

Çağlar Arlı      -    5 Views

OpenSSL: best way to get sha256 hash of two sha256 hashes

I have two sha256 hashes as hex strings

HASH1=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
HASH2=7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730

now I want to create a new SHA256 that depends on these two hashes (for a merkle-tree structure). Any two hex representations of the same hashes should in any case always result in a representation of the same binary hash (so for example if I go off the string representations, then it must be some canonical form of it, like ascii encoded lower case)

I know that I need to use an injective function, but what's the best way to go?

should I use the string values and some delimiter and then hash the binary representation of that string (and rely on bash using ASCII encodinng for it)?

NEW_HASH=$(echo -n "${HASH1,,},${HASH2,,}" | openssl dgst -sha256 | sed 's/(stdin)= //')

Or is it better to convert the hex representations to binary first and then just concatenate the binary representations (since the two hashes have fixed length, the result would be unambiguous)?

BIN1=$(echo -n "$HASH1" | xxd -r -p -)
BIN2=$(echo -n "$HASH2" | xxd -r -p -)
NEW_HASH=$(echo -n "$BIN1$BIN2" | openssl dgst -sha256 | sed 's/(stdin)= //')

What's the better approach and why?