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 :
35 : void
36 0 : init_gcrypt (void)
37 : {
38 0 : if (!gcry_check_version (GCRYPT_VERSION)) {
39 0 : fputs ("libgcrypt version mismatch\n", stderr);
40 0 : exit (2);
41 : }
42 :
43 0 : gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
44 :
45 : /* Allocate a pool of 16k secure memory. This make the secure memory
46 : * available and also drops privileges where needed. */
47 0 : gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
48 :
49 0 : gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
50 :
51 0 : gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
52 0 : }
53 :
54 : int
55 0 : main (int argc, char **argv)
56 : {
57 : gcry_md_hd_t hd;
58 : gcry_error_t err;
59 : int algo;
60 :
61 0 : init_gcrypt();
62 :
63 0 : if (argc < 2 || (argv[1] && !strcmp(argv[1], "--help")))
64 : {
65 0 : fprintf (stderr, "Usage: %s <digest> <file>...\n", argv[0]);
66 0 : return 1;
67 : }
68 :
69 0 : algo = gcry_md_map_name (argv[1]);
70 0 : if (algo == GCRY_MD_NONE)
71 : {
72 0 : fprintf (stderr, "Unknown algorithm '%s'\n", argv[1]);
73 0 : return 1;
74 : }
75 :
76 0 : err = gcry_md_open(&hd, algo, 0);
77 0 : if (err)
78 : {
79 0 : fprintf (stderr, "LibGCrypt error %s/%s\n",
80 : gcry_strsource (err),
81 : gcry_strerror (err));
82 0 : exit (1);
83 : }
84 :
85 0 : for (argv += 2; *argv; argv++)
86 : {
87 : FILE *fp;
88 : unsigned char buf[1024];
89 : size_t size;
90 : int i;
91 : unsigned char *h;
92 0 : if (!strcmp (*argv, "-"))
93 0 : fp = stdin;
94 : else
95 0 : fp = fopen (*argv, "r");
96 :
97 0 : if (fp == NULL)
98 : {
99 0 : perror ("fopen");
100 0 : return 1;
101 : }
102 :
103 0 : while (!feof (fp))
104 : {
105 0 : size = fread (buf, 1, sizeof(buf), fp);
106 0 : gcry_md_write (hd, buf, size);
107 : }
108 :
109 0 : h = gcry_md_read(hd, 0);
110 :
111 0 : for (i = 0; i < gcry_md_get_algo_dlen (algo); i++)
112 0 : printf("%02x", h[i]);
113 0 : printf(" %s\n", *argv);
114 :
115 0 : gcry_md_reset(hd);
116 : }
117 :
118 0 : gcry_md_close(hd);
119 0 : return 0;
120 : }
|