You are not logged in.
Pages: 1
How can i generate a SHA256 hash for a message signed with a private key?
I have tried searching for library and functions online. I can see functions like sha256sum but i can't find how to get the signing done with a private key and generate the hash.
My philosophy, in essence, is the concept of man as a heroic being, with his own happiness as the moral purpose of his life, with productive achievement as his noblest activity, and reason as his only absolute.
Offline
Check out libgcrypt: http://www.gnu.org/software/libgcrypt/
It has a lot of great cryptographic support, and I have used it before to generate MD5 digital signatures. It is not a very easy library to use, but I was able to make it work for me.
Offline
OpenSSL also has SHA-256 functions, although I can't seem to find man pages for them which is super lame. There's one for MD{2,4,5} and one for SHA-1. The interface is same, so just replace SHA1_* with SHA256_*:
/*
* compile with:
* cc test.c -lcrypto
*/
#include <openssl/sha.h>
#include <stdio.h>
int main(int argc, char **argv)
{
unsigned char buffer[BUFSIZ];
FILE *f;
SHA256_CTX ctx;
size_t len;
if (argc < 2) {
fprintf(stderr, "usage: %s <file>\n", argv[0]);
return 1;
}
f = fopen(argv[1], "r");
if (!f) {
fprintf(stderr, "couldn't open %s\n", argv[1]);
return 1;
}
SHA256_Init(&ctx);
do {
len = fread(buffer, 1, BUFSIZ, f);
SHA256_Update(&ctx, buffer, len);
} while (len == BUFSIZ);
SHA256_Final(buffer, &ctx);
fclose(f);
for (len = 0; len < SHA256_DIGEST_LENGTH; ++len)
printf("%02x", buffer[len]);
putchar('\n');
return 0;
}
It may also have functions more specific to exactly whatever it is you're trying to implement. Take a look at "man 3ssl crypto".
Last edited by cmtptr (2013-05-27 13:27:30)
Offline
Pages: 1