OpenSSL has terrible documentation with no code examples, but here you are:
#include <openssl/sha.h> bool simpleSHA256(void* input, unsigned long length, unsigned char* md) { SHA256_CTX context; if(!SHA256_Init(&context)) return false; if(!SHA256_Update(&context, (unsigned char*)input, length)) return false; if(!SHA256_Final(md, &context)) return false; return true; }
Using:
unsigned char md[SHA256_DIGEST_LENGTH];
Subsequently, md will contain a binary SHA-256 message digest. Similar code can be used for other members of the SHA family, just replace “256” in the code.
If you have larger data, you should of course feed the chunks of data as they arrive (several calls to SHA256_Update ).
AndiDog Feb 14 '10 at 19:34 2010-02-14 19:34
source share