#include <stdio.h>
#include <sha1.h>

unsigned char buffer[64];

int main(int argc, char *argv[])
{
    SHA1Context sha;
    FILE *fp;
    int i;
    unsigned char hash[20];

    if ((fp = fopen(argv[1], "r")) == NULL) {
        perror("fopen");
        return -1;
    }

    SHA1Reset(&sha);
    SHA1SetNoPadding(&sha);
    while (fread(buffer, sizeof(buffer), 1, fp) == 1) {
        SHA1Input(&sha, buffer, sizeof(buffer));
    }
    SHA1Result(&sha, hash);

    if (argc == 3) {
        hash[6] ^= 1 << 3;
    }

    for (i = 0; i < sizeof(hash); ++i) {
        printf("%02x", hash[i]);
    }
    for (i = 0; i < sizeof(hash); ++i) {
        printf("%02x", ~hash[i] & 0xff);
    }
    printf("\n");

    fclose(fp);

    return 0;
}
