X-Git-Url: https://pd.if.org/git/?p=zpackage;a=blobdiff_plain;f=src%2Fhash.c;h=498a7a1c83ed083619dbca85edf7dfbed7d00f61;hp=46c99d28399b5b352deef7c15d532ac99c82e26b;hb=2ac486ab18adbbb84563eafc0d67fa8da6ca7822;hpb=4fb490d9107b747c86964d0d3925470b06d97c8c diff --git a/src/hash.c b/src/hash.c index 46c99d2..498a7a1 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,49 +1,92 @@ #include #include #include +#include +#include +#include #include "zpm.h" #include "sha256.h" +#include "lib/blake2/ref/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; - /* - * hash stdin - */ - if (ac == 1 || (ac == 2 && !strcmp(av[1], "-"))) { - struct sha256_state md; - unsigned char buf[4096]; - size_t bytes; - unsigned char tmp[32]; - int j; - - sha256_init(&md); - do { - bytes = fread(buf, 1, sizeof buf, stdin); - sha256_process(&md, buf, bytes); - } while (bytes && !feof(stdin)); - if (ferror(stdin)) { - exit(1); + 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); + } } - sha256_done(&md, tmp); - for (j=0;j<32;j++) { - sprintf(hash+j*2, "%02x", (unsigned)tmp[j]); + rv = hash_file(input, hash); + close(input); + if (rv == 0) { + perror("zpm-hash2"); + exit(EXIT_FAILURE); } - hash[64] = 0; printf("%s\n", hash); - return 0; } if (ac < 2) { - fprintf(stderr, "usage: path\n"); - return 1; - } - rv = zpm_hash(av[1], hash, 0); - if (rv) { + filename = "-"; + rv = hash_file(0, hash); + if (rv == 0) { + perror("zpm-hash3"); + exit(EXIT_FAILURE); + } printf("%s\n", hash); } - return !rv; + return 0; }