]> pd.if.org Git - zpackage/blobdiff - src/hash.c
switch to blake2
[zpackage] / src / hash.c
index 46c99d28399b5b352deef7c15d532ac99c82e26b..498a7a1c83ed083619dbca85edf7dfbed7d00f61 100644 (file)
@@ -1,49 +1,92 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #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;
 }