3 * $Id: hashsum.c,v 1.7 2001/02/21 20:03:22 mdw Exp $
5 * Hash files using some secure hash function
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.7 2001/02/21 20:03:22 mdw
34 * Added support for MD2 hash function.
36 * Revision 1.6 2001/01/25 21:40:14 mdw
37 * Support for new SHA variants added.
39 * Revision 1.5 2000/12/06 20:33:27 mdw
40 * Make flags be macros rather than enumerations, to ensure that they're
43 * Revision 1.4 2000/08/04 23:23:44 mdw
44 * Various <ctype.h> fixes.
46 * Revision 1.3 2000/07/29 17:02:43 mdw
47 * (checkhash): Be pettier about spaces between the hash and filename, for
48 * compatiblity with `md5sum'.
50 * Revision 1.2 2000/07/15 21:14:05 mdw
51 * Missed `-e' out of the usage string.
53 * Revision 1.1 2000/07/15 20:52:34 mdw
54 * Useful replacement for `md5sum' with support for many different hash
55 * functions and for reading filename lists from `find'.
59 /*----- Header files ------------------------------------------------------*/
69 #include <mLib/alloc.h>
70 #include <mLib/dstr.h>
71 #include <mLib/mdwopt.h>
72 #include <mLib/quis.h>
73 #include <mLib/report.h>
92 /*----- Static variables --------------------------------------------------*/
94 static const gchash
*hashtab
[] = {
96 &sha
, &sha256
, &sha384
, &sha512
,
97 &rmd128
, &rmd160
, &rmd256
, &rmd320
,
108 #define f_oddhash 64u
109 #define f_escape 128u
111 /*----- Support functions -------------------------------------------------*/
115 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
116 * @unsigned f@ = flags to set
117 * @const gchash *gch@ = pointer to hash function to use
118 * @void *buf@ = pointer to hash output buffer
120 * Returns: Zero if it worked, nonzero on error.
122 * Use: Hashes a file.
125 static int fhash(const char *file
, unsigned f
, const gchash
*gch
, void *buf
)
135 else if ((fp
= fopen(file
, f
& f_binary ?
"rb" : "r")) == 0)
139 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0)
140 h
->ops
->hash(h
, fbuf
, sz
);
141 h
->ops
->done(h
, buf
);
149 /* --- @puthex@ --- *
151 * Arguments: @const octet *buf@ = pointer to a binary buffer
152 * @size_t sz@ = size of the buffer
153 * @FILE *fp@ = pointer to output file handle
157 * Use: Writes a hex dump of a block of memory.
160 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
163 fprintf(fp
, "%02x", *buf
++);
168 /* --- @gethex@ --- *
170 * Arguments: @const char *p@ = pointer to input string
171 * @octet *q@ = pointer to output buffer
172 * @size_t sz@ = size of the output buffer
173 * @char **pp@ = where to put the end pointer
175 * Returns: The number of bytes written to the buffer.
177 * Use: Reads hex dumps from the input string.
180 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
184 isxdigit((unsigned char)p
[0]) &&
185 isxdigit((unsigned char)p
[1])) {
190 *q
++ = strtoul(buf
, 0, 16);
200 /* --- @gethash@ --- *
202 * Arguments: @const char *name@ = pointer to name string
204 * Returns: Pointer to appropriate hash class.
206 * Use: Chooses a hash function by name.
209 static const gchash
*gethash(const char *name
)
211 const gchash
**g
, *gg
= 0;
212 size_t sz
= strlen(name
);
213 for (g
= hashtab
; *g
; g
++) {
214 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
215 if ((*g
)->name
[sz
] == 0) {
227 /* --- @getstring@ --- *
229 * Arguments: @FILE *fp@ = stream from which to read
230 * @const char *p@ = string to read from instead
231 * @dstr *d@ = destination string
232 * @unsigned raw@ = raw or cooked read
234 * Returns: Zero if OK, nonzero on end-of-file.
236 * Use: Reads a filename (or something similar) from a stream.
239 static int getstring(FILE *fp
, const char *p
, dstr
*d
, unsigned raw
)
244 /* --- Raw: just read exactly what's written up to a null byte --- */
246 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
247 #define EOFCH (fp ? EOF : 0)
250 if ((ch
= NEXTCH
) == EOFCH
)
256 if ((ch
= NEXTCH
) == EOFCH
)
263 /* --- Skip as far as whitespace --- *
265 * Also skip past comments.
273 do ch
= NEXTCH
; while (ch
!= '\n' && ch
!= EOFCH
);
279 /* --- If the character is a quote then read a quoted string --- */
291 /* --- Now read all sorts of interesting things --- */
295 /* --- Handle an escaped thing --- */
302 case 'a': ch
= '\a'; break;
303 case 'b': ch
= '\b'; break;
304 case 'f': ch
= '\f'; break;
305 case 'n': ch
= '\n'; break;
306 case 'r': ch
= '\r'; break;
307 case 't': ch
= '\t'; break;
308 case 'v': ch
= '\v'; break;
315 /* --- If it's a quote or some other end marker then stop --- */
319 if (!q
&& isspace(ch
))
322 /* --- Otherwise contribute and continue --- */
325 if ((ch
= NEXTCH
) == EOFCH
)
338 /* --- @putstring@ --- *
340 * Arguments: @FILE *fp@ = stream to write on
341 * @const char *p@ = pointer to text
342 * @unsigned raw@ = whether the string is to be written raw
346 * Use: Emits a string to a stream.
349 static void putstring(FILE *fp
, const char *p
, unsigned raw
)
351 size_t sz
= strlen(p
);
355 /* --- Just write the string null terminated if raw --- */
358 fwrite(p
, 1, sz
+ 1, fp
);
362 /* --- Check for any dodgy characters --- */
365 for (q
= p
; *q
; q
++) {
366 if (isspace((unsigned char)*q
)) {
375 /* --- Emit the string --- */
377 for (q
= p
; *q
; q
++) {
379 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
380 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
381 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
382 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
383 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
384 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
385 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
386 case '`': fputc('\\', fp
); fputc('`', fp
); break;
387 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
388 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
389 case '#': fputc('\\', fp
); fputc('#', fp
); break;
402 /*----- Guts --------------------------------------------------------------*/
404 static int checkhash(const char *file
, unsigned f
, const gchash
*gch
)
410 unsigned long n
= 0, nfail
= 0;
411 octet
*buf
= xmalloc(2 * gch
->hashsz
);
415 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
416 moan("couldn't open `%s': %s", file
, strerror(errno
));
417 return (EXIT_FAILURE
);
420 while (DRESET(&d
), dstr_putline(&d
, fp
) != EOF
) {
425 /* --- Handle a directive --- */
429 if ((q
= str_getword(&p
)) == 0)
431 if (strcmp(q
, "hash") == 0) {
433 if ((q
= str_getword(&p
)) == 0)
435 if ((g
= gethash(q
)) == 0)
439 buf
= xmalloc(2 * gch
->hashsz
);
440 } else if (strcmp(q
, "escape") == 0)
445 /* --- Otherwise it's a hex thing --- */
448 while (*p
&& *p
!= ' ')
453 if (gethex(q
, buf
, gch
->hashsz
, 0) < gch
->hashsz
)
463 getstring(0, p
, &dd
, 0);
467 if (fhash(p
, ff
, gch
, buf
+ gch
->hashsz
)) {
468 moan("couldn't read `%s': %s", p
, strerror(errno
));
472 if (memcmp(buf
, buf
+ gch
->hashsz
, gch
->hashsz
) != 0) {
474 fprintf(stderr
, "FAIL %s\n", p
);
476 moan("%s check failed for `%s'", gch
->name
, p
);
481 fprintf(stderr
, "OK %s\n", p
);
489 if ((f
& f_verbose
) && nfail
)
490 moan("%lu of %lu file(s) failed %s check", nfail
, n
, gch
->name
);
492 moan("no files checked");
496 static int dohash(const char *file
, unsigned f
, const gchash
*gch
)
499 octet
*p
= xmalloc(gch
->hashsz
);
501 if (fhash(file
, f
, gch
, p
)) {
502 moan("couldn't read `%s': %s", file ? file
: "<stdin>", strerror(errno
));
505 puthex(p
, gch
->hashsz
, stdout
);
508 fputc(f
& f_binary ?
'*' : ' ', stdout
);
510 putstring(stdout
, file
, 0);
521 static int dofile(const char *file
, unsigned f
, const gchash
*gch
)
523 return (f
& f_check ? checkhash
: dohash
)(file
, f
, gch
);
526 static int hashfiles(const char *file
, unsigned f
, const gchash
*gch
)
535 else if ((fp
= fopen(file
, f
& f_raw ?
"r" : "rb")) == 0) {
536 moan("couldn't open `%s': %s", file
, strerror(errno
));
537 return (EXIT_FAILURE
);
542 if (getstring(fp
, 0, &d
, f
& f_raw
))
544 if ((rrc
= dofile(d
.buf
, f
, gch
)) != 0)
551 static int hashsum(const char *file
, unsigned f
, const gchash
*gch
)
553 return (f
& f_files ? hashfiles
: dofile
)(file
, f
, gch
);
556 /*----- Main driver -------------------------------------------------------*/
558 static void version(FILE *fp
)
560 pquis(fp
, "$, Catacomb version " VERSION
"\n");
563 static void usage(FILE *fp
)
565 pquis(fp
, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
568 static void help(FILE *fp
, const gchash
*gch
)
574 Generates or checks message digests on files. Options available:\n\
576 -h, --help Display this help message.\n\
577 -V, --version Display program's version number.\n\
578 -u, --usage Display a terse usage message.\n\
580 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
582 -f, --files Read a list of file names from standard input.\n\
583 -0, --null File names are null terminated, not plain text.\n\
585 -e, --escape Escape funny characters in filenames.\n\
586 -c, --check Check message digests rather than emitting them.\n\
587 -b, --binary When reading files, treat them as binary.\n\
588 -v, --verbose Be verbose when checking digests.\n\
590 For a list of supported message digest algorithms, type `$ --list'.\n\
593 fprintf(fp
, "The default message digest algorithm is %s.\n", gch
->name
);
596 int main(int argc
, char *argv
[])
599 const gchash
*gch
= 0;
602 /* --- Initialization --- */
607 /* --- Choose a hash function from the name --- */
610 char *q
= xstrdup(QUIS
);
611 size_t len
= strlen(q
);
612 if (len
> 3 && strcmp(q
+ len
- 3, "sum") == 0) {
621 /* --- Read options --- */
624 static struct option opts
[] = {
625 { "help", 0, 0, 'h' },
626 { "verbose", 0, 0, 'V' },
627 { "usage", 0, 0, 'u' },
629 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
630 { "hash", OPTF_ARGREQ
, 0, 'a' },
631 { "list", 0, 0, 'l' },
633 { "files", 0, 0, 'f' },
634 { "find", 0, 0, 'f' },
635 { "null", 0, 0, '0' },
637 { "escape", 0, 0, 'e' },
638 { "check", 0, 0, 'c' },
639 { "binary", 0, 0, 'b' },
640 { "verbose", 0, 0, 'v' },
644 int i
= mdwopt(argc
, argv
, "hVu a:l f0 ecbv", opts
, 0, 0, 0);
659 if ((gch
= gethash(optarg
)) == 0)
660 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
665 for (j
= 0; hashtab
[j
]; j
++) {
668 printf("%s", hashtab
[j
]->name
);
704 /* --- Generate output --- */
706 if (!(f
& f_check
)) {
708 printf("#hash %s\n", gch
->name
);
710 fputs("#escape\n", stdout
);
717 for (i
= 0; i
< argc
; i
++) {
718 if ((rrc
= hashsum(argv
[i
], f
, gch
)) != 0)
722 rc
= hashsum(0, f
, gch
);
727 /*----- That's all, folks -------------------------------------------------*/