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