amosnier / sha-2

SHA-2 algorithm implementations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting a different output compared to sha256sum command

demhademha opened this issue · comments

I have the following code - all seems to be ok:

#include "sha-256.h"
#include <stdio.h>
#define HASH_LENGTH 32
int main(void)
{
	uint8_t hash[HASH_LENGTH];
	calc_sha_256(hash, "abc", strlen("abc"));
	size_t i;
	for (i = 0; i < HASH_LENGTH; i++)
	{
		printf("%02x", hash[i]);
	}
	puts(""); //new line here
}

However, the sha256sum utility is producing a different output.
what could possibly be wrong with my code?
Regards

Compiling your program and testing it, I get:

$ ./main
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

Creating a file with the same contents to compare with sha256sum, I get:

$ echo -n abc > tmp.txt
$ sha256sum tmp.txt
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad tmp.txt

Looks consistent to me. You must be making a mistake somewhere, like having a line feed in your text file.

For the record, you do not need to redefine HASH_LENGTH. You can just use SIZE_OF_SHA_256_HASH instead.