views:

49

answers:

0

Hey there. I'm having a very strange problem with creating sha256 hashes. I made a simple C console program that takes a file path as an argument and uses the standalone sha256 code that can be found here. I compiled the program using MinGW 5.1.6 on Windows 7 x64.

When testing the program on a file, the resultant hash is wrong. I made sure of this by using md5deep on the file, and then by using sha256sum on the file under Linux. I also verified it was not the code by compiling and running the same code on my Linux box with the same file; the hash it produced was identical to the ones produced by md5deep and sha256sum.

I also adapted Aaron Gifford's sha256 implementation into a different version of my simple program and performed the test again on both Windows and Linux and ended up with the same result.

Could it be possible that the issue is being caused by compiler flags that have not been switched on?

My knowledge of C isn't amazing and my knowledge of compiler options is even worse, so any help would be kindly appreciated.

The code for the simple program is below:

#include <stdio.h>
#include "sha256.h"

#define BUFLEN 16384

int main(int argc, char *argv[]) {
    sha256_context ctx256;
    sha256_starts(&ctx256);
    int kl, l, fd;
    unsigned char buf[BUFLEN];
    FILE *file = (FILE*) 0;
    char *filepath;

    fd = fileno(stdin);

    filepath = argv[1];
    file = fopen(filepath, "r");

    fd = fileno(file);
    while ((l = read(fd, buf, BUFLEN)) > 0) {
        kl += l;
        sha256_update(&ctx256, buf, l);
    }
    fclose(file);
    uint8 sha256sum[32];
    sha256_finish(&ctx256, sha256sum);
    int i;
    for (i = 0; i < 32; i++) {
        printf("%02x", sha256sum[i]);
    }
    printf("\n");

    return 0;
}