#include #include #include #include #include #include #include "zpm.h" #include "sha256.h" #include "blake2.h" static int hash_file(int fd, char *hash) { unsigned char buf[4096]; ssize_t bytes; unsigned char tmp[32]; int j; #if 0 struct sha256_state md; sha256_init(&md); do { bytes = read(fd, buf, sizeof buf); if (bytes == -1) { return 0; } sha256_process(&md, buf, bytes); } while (bytes); sha256_done(&md, tmp); for (j=0;j<32;j++) { sprintf(hash+j*2, "%02x", (unsigned)tmp[j]); } hash[64] = 0; #else struct blake2b_state__ blake; blake2b_init(&blake, sizeof tmp); do { bytes = read(fd, buf, sizeof buf); if (bytes == -1) { return 0; } blake2b_update(&blake, buf, bytes); } while (bytes); blake2b_final(&blake, tmp, sizeof tmp); for (j=0;j<32;j++) { sprintf(hash+j*2, "%02x", (unsigned)tmp[j]); } hash[64] = 0; #endif return 1; } int main(int ac, char **av){ int rv; char hash[65]; int input; char *filename; int i; for (i=1; i < ac; i++) { filename = av[i]; if (strcmp(filename, "-") == 0) { input = 0; } else { input = open(filename, O_RDONLY); if (input == -1) { fprintf(stderr, "%s ", filename); perror("zpm-hash1"); exit(EXIT_FAILURE); } } rv = hash_file(input, hash); close(input); if (rv == 0) { perror("zpm-hash2"); exit(EXIT_FAILURE); } printf("%s\n", hash); } if (ac < 2) { filename = "-"; rv = hash_file(0, hash); if (rv == 0) { perror("zpm-hash3"); exit(EXIT_FAILURE); } printf("%s\n", hash); } return 0; }