sha256

Simple C SHA256 implementation. Author: Brad Conte
git clone https://noulin.net/git/sha256.git
Log | Files | Refs | LICENSE

sha256.c (757B)


      1 /*
      2  * Author: Remy Noulin
      3  * License: MIT
      4  */
      5 
      6 #include "bconSha256.h"
      7 #include "sha256.h"
      8 #include "stdlib.h"
      9 #include "string.h"
     10 
     11 char *sha256S(const char *bufferToHash);
     12 char *sha256_byteToHexString(BYTE data[]);
     13 
     14 char *sha256S(const char *bufferToHash) {
     15 	if (!bufferToHash) return NULL;
     16 
     17 	SHA256_CTX ctx;
     18 	sha256_init(&ctx);
     19 	sha256_update(&ctx, bufferToHash, strlen(bufferToHash));
     20 	BYTE result[32];
     21 	sha256_final(&ctx, result);
     22 
     23 	char *hexS = sha256_byteToHexString(result);
     24 	return hexS;
     25 }
     26 
     27 char *sha256_byteToHexString(BYTE data[]) {
     28 	char *hexC = "0123456789abcdef";
     29 	char *hexS = malloc(65);
     30 	if (!hexS) return NULL;
     31 	for(BYTE i; i<32; i++) {
     32 		hexS[i*2]   = hexC[data[i]>>4];
     33 		hexS[i*2+1] = hexC[data[i]&0xF];
     34 	}
     35 	hexS[64] = 0;
     36 	return hexS;
     37 }