SHA512/256 in libssl

By steve, 24 May, 2017

Libssl does not support SHA512/256 natively, but these hashes can be calculated using the following code:

#include <openssl/sha.h>

int main() {
        unsigned char hash[32];
        SHA512_CTX libssl_sha512;
        unsigned char *data="Test data";
        int datalen=strlen(data);

        // Init a SHA512 hash structure
        SHA512_Init(&libssl_sha512);

        // Modify the initial hash structure with the correct magic numbers for SHA512/256
        libssl_sha512.h[0]=0x22312194FC2BF72C;
        libssl_sha512.h[1]=0x9F555FA3C84C64C2;
        libssl_sha512.h[2]=0x2393B86B6F53B151;
        libssl_sha512.h[3]=0x963877195940EABD;
        libssl_sha512.h[4]=0x96283EE2A88EFFE3;
        libssl_sha512.h[5]=0xBE5E1E2553863992;
        libssl_sha512.h[6]=0x2B0199FC2C85B8AA;
        libssl_sha512.h[7]=0x0EB72DDC81C52CA2;

        // Send data to the hash function in pieces.  Repeat as needed
        SHA512_Update(&libssl_sha512, data, datalen);

        // Generate the hash of the data sent in the previous step and save it in the hash variable
        SHA512_Final(hash, &libssl_sha512);
}

The above generated the correct hash for the blank string.

Comments