]> pd.if.org Git - zpackage/blob - zpm-hash.c
add hashing stdin to zpm-hash
[zpackage] / zpm-hash.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include "zpm.h"
4 #include "sha256.h"
5
6 int main(int ac, char **av){
7         int rv;
8         char hash[65];
9
10         /*
11          * hash stdin
12          */
13         if (ac == 1 || (ac == 2 && !strcmp(av[1], "-"))) {
14                 hash_state md;
15                 unsigned char buf[4096];
16                 size_t bytes;
17                 unsigned char tmp[32];
18                 int j;
19
20                 sha256_init(&md);
21                 do {
22                         bytes = fread(buf, 1, sizeof buf, stdin);
23                         sha256_process(&md, buf, bytes);
24                 } while (bytes && !feof(stdin));
25                 if (ferror(stdin)) {
26                         exit(1);
27                 }
28                 sha256_done(&md, tmp);
29                 for (j=0;j<32;j++) {
30                         sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
31                 }
32                 hash[64] = 0;
33                 printf("%s\n", hash);
34                 return 0;
35         }
36
37         if (ac < 2) {
38                 fprintf(stderr, "usage: path\n");
39                 return 1;
40         }
41         rv = zpm_hash(av[1], hash, 0);
42         if (rv) {
43                 printf("%s\n", hash);
44         }
45
46         return rv;
47 }