5 Copyright (C) 2011, 2013 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
43 #include "nettle-meta.h"
52 list_algorithms (void)
55 const struct nettle_hash *alg;
56 printf ("%10s digestsize (internal block size), in units of octets\n", "name");
58 for (i = 0; (alg = nettle_hashes[i]); i++)
59 printf ("%10s %d (%d)\n",
60 alg->name, alg->digest_size, alg->block_size);
63 static const struct nettle_hash *
64 find_algorithm (const char *name)
66 const struct nettle_hash *alg;
69 for (i = 0; (alg = nettle_hashes[i]); i++)
70 if (!strcmp(name, alg->name))
76 /* Also in examples/io.c */
78 hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
83 size_t res = fread(buffer, 1, sizeof(buffer), f);
87 hash->update(ctx, res, buffer);
94 digest_file(const struct nettle_hash *alg,
95 unsigned digest_length, int raw,
100 ctx = xalloc(alg->context_size);
104 if (!hash_file (alg, ctx, f))
110 digest = xalloc(digest_length);
111 alg->digest(ctx, digest_length, digest);
115 fwrite (digest, digest_length, 1, stdout);
120 char hex[BASE16_ENCODE_LENGTH(8) + 1];
121 for (i = 0; i + 8 < digest_length; i += 8)
123 base16_encode_update(hex, 8, digest + i);
124 hex[BASE16_ENCODE_LENGTH(8)] = 0;
127 base16_encode_update(hex, digest_length - i, digest + i);
128 hex[BASE16_ENCODE_LENGTH(digest_length - i)] = 0;
129 printf("%s %s\n", hex, alg->name);
140 fprintf(f, "Usage: nettle-hash -a ALGORITHM [OPTIONS] [FILE ...]\n"
142 " --help Show this help.\n"
143 " -V, --version Show version information.\n"
144 " --list List supported hash algorithms.\n"
145 " -a, --algorithm=ALG Hash algorithm to use.\n"
146 " -l, --length=LENGTH Desired digest length (octets)\n"
147 " --raw Raw binary output.\n");
150 /* FIXME: Be more compatible with md5sum and sha1sum. Options -c
151 (check), -b (binary), -t (text), and output format with hex hash
152 sum, optional star (meaning binary mode), and file name. */
154 main (int argc, char **argv)
156 const char *alg_name = NULL;
157 const struct nettle_hash *alg;
162 enum { OPT_HELP = 0x300, OPT_RAW, OPT_LIST };
163 static const struct option options[] =
165 /* Name, args, flag, val */
166 { "help", no_argument, NULL, OPT_HELP },
167 { "version", no_argument, NULL, 'V' },
168 { "algorithm", required_argument, NULL, 'a' },
169 { "length", required_argument, NULL, 'l' },
170 { "list", no_argument, NULL, OPT_LIST },
171 { "raw", no_argument, NULL, OPT_RAW },
176 while ( (c = getopt_long(argc, argv, "Va:l:", options, NULL)) != -1)
188 printf("nettle-hash (" PACKAGE_STRING ")\n");
198 die ("Invalid length argument: `%s'\n", optarg);
211 die("Algorithm argument (-a option) is mandatory.\n"
212 "See nettle-hash --help for further information.\n");
214 alg = find_algorithm (alg_name);
216 die("Hash algorithm `%s' not supported or .\n"
217 "Use nettle-hash --list to list available algorithms.\n",
221 length = alg->digest_size;
222 else if (length > alg->digest_size)
223 die ("Length argument %d too large for selected algorithm.\n",
230 digest_file (alg, length, raw, stdin);
234 for (i = 0; i < argc; i++)
236 FILE *f = fopen (argv[i], "rb");
238 die ("Cannot open `%s': %s\n", argv[i], STRERROR(errno));
239 printf("%s: ", argv[i]);
240 if (!digest_file (alg, length, raw, f))
241 die("Reading `%s' failed: %s\n", argv[i], STRERROR(errno));
245 if (fflush(stdout) != 0 )
246 die("Write failed: %s\n", STRERROR(errno));