I am writing an application, which requires, some data to be hashed using SHA-256 algorithm. I knew that, it is supported by openssl, and hence was searching for the library functions. I tried several searches specific to sha256 and openssl, but didn't come up with anything concrete. Then I tried, simple sha and openssl search and I got `SHA1_*' series of functions, which is not what I wanted. Then, I got an idea to search for `SHA256_*' set of functions, and I got it.
Next, I tried for the man pages, in my system, which I got for SHA1_* functions, but not for SHA256_init. I could have used the searched links directly, but I was afraid to come across an example, and I didn't want to loose the opportunity to discover use of the library. When, I didn't get the man pages, I was a bit upset, on why it was not available, and was thinking, if there was any other way to do it. Then, I realized, why not look into the header files for the function prototypes. And bingo. I got it, tried out the example and there I am with the output. Not really, I first got the binary output, which I then converted to hex.
Here is my example code:
#include <iostream>
#include <sys/types.h>
#include <openssl/sha.h>
using namespace std;
int main()
{
unsigned char digest[SHA256_DIGEST_LENGTH] = {'\0'};
string data = "1yard2mop";
SHA256_CTX c;
SHA256_Init(&c);
SHA256_Update(&c, data.c_str(), data.length());
SHA256_Final(digest, &c);
for(size_t i=0;i < SHA256_DIGEST_LENGTH;i++)
printf("%x", digest[i]);
cout << endl;
return 0;
}
In the end, I was laughing at myself on the whole `discovery part'.
Current music: Thozhiya En Kadhaliya