You are not logged in.

#1 2013-05-17 07:35:04

Sanchayan
Member
From: Bombay, India
Registered: 2012-09-11
Posts: 14
Website

Help required with SHA256 in C

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

#2 2013-05-23 19:11:19

johni
Member
Registered: 2012-02-03
Posts: 102

Re: Help required with SHA256 in C

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

#3 2013-05-27 13:18:42

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: Help required with SHA256 in C

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

Board footer

Powered by FluxBB