Line data Source code
1 : /* gchash.c - Calculate hash values
2 : * Copyright (C) 2013 Dmitry Eremin-Solenikov
3 : *
4 : * This file is part of Libgcrypt.
5 : *
6 : * Libgcrypt is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * Libgcrypt is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #ifdef HAVE_CONFIG_H
21 : # include <config.h>
22 : #endif
23 : #include <stdio.h>
24 : #include <stdlib.h>
25 : #include <ctype.h>
26 :
27 : #ifdef _GCRYPT_IN_LIBGCRYPT
28 : # undef _GCRYPT_IN_LIBGCRYPT
29 : # include "gcrypt.h"
30 : #else
31 : # include <gcrypt.h>
32 : #endif
33 :
34 : #define PGM "gchash"
35 : #include "t-common.h"
36 :
37 :
38 : void
39 0 : init_gcrypt (void)
40 : {
41 0 : if (!gcry_check_version (GCRYPT_VERSION)) {
42 0 : fputs ("libgcrypt version mismatch\n", stderr);
43 0 : exit (2);
44 : }
45 :
46 0 : xgcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
47 :
48 : /* Allocate a pool of 16k secure memory. This make the secure memory
49 : * available and also drops privileges where needed. */
50 0 : xgcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
51 :
52 0 : xgcry_control (GCRYCTL_RESUME_SECMEM_WARN);
53 :
54 0 : xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
55 0 : }
56 :
57 : int
58 0 : main (int argc, char **argv)
59 : {
60 : gcry_md_hd_t hd;
61 : gcry_error_t err;
62 : int algo;
63 :
64 0 : init_gcrypt();
65 :
66 0 : if (argc < 2 || (argv[1] && !strcmp(argv[1], "--help")))
67 : {
68 0 : fprintf (stderr, "Usage: %s <digest> <file>...\n", argv[0]);
69 0 : return 1;
70 : }
71 :
72 0 : algo = gcry_md_map_name (argv[1]);
73 0 : if (algo == GCRY_MD_NONE)
74 : {
75 0 : fprintf (stderr, "Unknown algorithm '%s'\n", argv[1]);
76 0 : return 1;
77 : }
78 :
79 0 : err = gcry_md_open(&hd, algo, 0);
80 0 : if (err)
81 : {
82 0 : fprintf (stderr, "LibGCrypt error %s/%s\n",
83 : gcry_strsource (err),
84 : gcry_strerror (err));
85 0 : exit (1);
86 : }
87 :
88 0 : for (argv += 2; *argv; argv++)
89 : {
90 : FILE *fp;
91 : unsigned char buf[1024];
92 : size_t size;
93 : int i;
94 : unsigned char *h;
95 0 : if (!strcmp (*argv, "-"))
96 0 : fp = stdin;
97 : else
98 0 : fp = fopen (*argv, "r");
99 :
100 0 : if (fp == NULL)
101 : {
102 0 : perror ("fopen");
103 0 : return 1;
104 : }
105 :
106 0 : while (!feof (fp))
107 : {
108 0 : size = fread (buf, 1, sizeof(buf), fp);
109 0 : gcry_md_write (hd, buf, size);
110 : }
111 :
112 0 : h = gcry_md_read(hd, 0);
113 :
114 0 : for (i = 0; i < gcry_md_get_algo_dlen (algo); i++)
115 0 : printf("%02x", h[i]);
116 0 : printf(" %s\n", *argv);
117 :
118 0 : gcry_md_reset(hd);
119 : }
120 :
121 0 : gcry_md_close(hd);
122 0 : return 0;
123 : }
|