]> pd.if.org Git - zpackage/commitdiff
add hashing stdin to zpm-hash
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 3 Mar 2017 11:47:58 +0000 (05:47 -0600)
committerNathan Wagner <nw@hydaspes.if.org>
Sun, 5 Mar 2017 15:05:13 +0000 (09:05 -0600)
t/hash.t
zpm-hash.c

index 48006dbc256882ded91deda129b4f435eccf8802..c727066d2d46b8f5bb82b1790682fb4269c048a0 100755 (executable)
--- a/t/hash.t
+++ b/t/hash.t
@@ -9,7 +9,7 @@ vtest() {
        okstreq "$res" "$2" "$1 == $2"
 }
 
-plan 2
+plan 4
 
 printf '' > hash.test
 vtest hash.test e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
@@ -18,5 +18,10 @@ vtest hash.test b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
 
 rm hash.test
 
+res=$(printf 'foo\n' | zpm-hash)
+okstreq $res b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
+
+res=$(printf 'foo\n' | zpm-hash -)
+okstreq $res b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
 
 finish
index 31791b184a197fd3f16c8b55e0b9b69aaae22616..f3578c76a7068a6a7b072709c261486ab715edb8 100644 (file)
@@ -1,11 +1,39 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include "zpm.h"
+#include "sha256.h"
 
 int main(int ac, char **av){
        int rv;
        char hash[65];
 
+       /*
+        * hash stdin
+        */
+       if (ac == 1 || (ac == 2 && !strcmp(av[1], "-"))) {
+               hash_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);
+               }
+               sha256_done(&md, tmp);
+               for (j=0;j<32;j++) {
+                       sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
+               }
+               hash[64] = 0;
+               printf("%s\n", hash);
+               return 0;
+       }
+
        if (ac < 2) {
                fprintf(stderr, "usage: path\n");
                return 1;