In python to obtain the digest of the byte string, I do this
hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
Result: 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
to reverse result, I do this
hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()[::-1]
Result: '2e109e97db885698cb12078e1e6dc055cd25f30c445cf8a54cb7334a'
I would like to do the same in C with Openssl.
I do this
#include <openssl/sha.h>
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
int main()
{
unsigned char digest[SHA224_DIGEST_LENGTH];
char string[] = "Nobody inspects the spammish repetition";
SHA224((unsigned char*)&string, strlen(string), (unsigned char*)&digest);
char mdString[SHA224_DIGEST_LENGTH * 2 + 1];
for (int i = 0; i < SHA224_DIGEST_LENGTH; i++)
sprintf(&mdString[i * 2], "%02x", (unsigned int)digest[i]);
printf("SHA224 digest: %s\n", mdString);
return EXIT_SUCCESS;
}
but I don't know how, with C++, to get the same result as the reverse in python.
How can I achieve this?
Any help would be greatly appreciated. Thank you.
Edit: it's not the same as using std::reverse
.
It is not a duplicate of the question How do you reverse a string in place in C or C++?
My solution: Ref: Swapping the nibbles in a char element
for (uint16_t j=0; j < SHA224_DIGEST_LENGTH; j++)
digest[j] = ((digest[j] & 0x0F) << 4) | ((digest[j] & 0xF0) >> 4);
std::reverse(std::begin(digest), std::end(digest));