1 /* Compute MD5, SHA1, SHA224, SHA256, SHA384 or SHA512 checksum of files or strings
2 Copyright (C) 1995-2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
23 #include <sys/types.h>
33 #if HASH_ALGO_SHA256 || HASH_ALGO_SHA224
36 #if HASH_ALGO_SHA512 || HASH_ALGO_SHA384
44 /* The official name of this program (e.g., no `g' prefix). */
46 # define PROGRAM_NAME "md5sum"
47 # define DIGEST_TYPE_STRING "MD5"
48 # define DIGEST_STREAM md5_stream
49 # define DIGEST_BITS 128
50 # define DIGEST_REFERENCE "RFC 1321"
51 # define DIGEST_ALIGN 4
53 # define PROGRAM_NAME "sha1sum"
54 # define DIGEST_TYPE_STRING "SHA1"
55 # define DIGEST_STREAM sha1_stream
56 # define DIGEST_BITS 160
57 # define DIGEST_REFERENCE "FIPS-180-1"
58 # define DIGEST_ALIGN 4
59 #elif HASH_ALGO_SHA256
60 # define PROGRAM_NAME "sha256sum"
61 # define DIGEST_TYPE_STRING "SHA256"
62 # define DIGEST_STREAM sha256_stream
63 # define DIGEST_BITS 256
64 # define DIGEST_REFERENCE "FIPS-180-2"
65 # define DIGEST_ALIGN 4
66 #elif HASH_ALGO_SHA224
67 # define PROGRAM_NAME "sha224sum"
68 # define DIGEST_TYPE_STRING "SHA224"
69 # define DIGEST_STREAM sha224_stream
70 # define DIGEST_BITS 224
71 # define DIGEST_REFERENCE "RFC 3874"
72 # define DIGEST_ALIGN 4
73 #elif HASH_ALGO_SHA512
74 # define PROGRAM_NAME "sha512sum"
75 # define DIGEST_TYPE_STRING "SHA512"
76 # define DIGEST_STREAM sha512_stream
77 # define DIGEST_BITS 512
78 # define DIGEST_REFERENCE "FIPS-180-2"
79 # define DIGEST_ALIGN 8
80 #elif HASH_ALGO_SHA384
81 # define PROGRAM_NAME "sha384sum"
82 # define DIGEST_TYPE_STRING "SHA384"
83 # define DIGEST_STREAM sha384_stream
84 # define DIGEST_BITS 384
85 # define DIGEST_REFERENCE "FIPS-180-2"
86 # define DIGEST_ALIGN 8
88 # error "Can't decide which hash algorithm to compile."
91 #define DIGEST_HEX_BYTES (DIGEST_BITS / 4)
92 #define DIGEST_BIN_BYTES (DIGEST_BITS / 8)
94 #define AUTHORS "Ulrich Drepper", "Scott Miller", "David Madore"
96 /* The minimum length of a valid digest line. This length does
97 not include any newline character at the end of a line. */
98 #define MIN_DIGEST_LINE_LENGTH \
99 (DIGEST_HEX_BYTES /* length of hexadecimal message digest */ \
100 + 2 /* blank and binary indicator */ \
101 + 1 /* minimum filename length */ )
103 /* True if any of the files read were the standard input. */
104 static bool have_read_stdin
;
106 /* The minimum length of a valid checksum line for the selected algorithm. */
107 static size_t min_digest_line_length
;
109 /* Set to the length of a digest hex string for the selected algorithm. */
110 static size_t digest_hex_bytes
;
112 /* With --check, don't generate any output.
113 The exit code indicates success or failure. */
114 static bool status_only
= false;
116 /* With --check, print a message to standard error warning about each
117 improperly formatted checksum line. */
118 static bool warn
= false;
120 /* The name this program was run with. */
123 /* For long options that have no equivalent short option, use a
124 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
127 STATUS_OPTION
= CHAR_MAX
+ 1
130 static const struct option long_options
[] =
132 { "binary", no_argument
, NULL
, 'b' },
133 { "check", no_argument
, NULL
, 'c' },
134 { "status", no_argument
, NULL
, STATUS_OPTION
},
135 { "text", no_argument
, NULL
, 't' },
136 { "warn", no_argument
, NULL
, 'w' },
137 { GETOPT_HELP_OPTION_DECL
},
138 { GETOPT_VERSION_OPTION_DECL
},
145 if (status
!= EXIT_SUCCESS
)
146 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
151 Usage: %s [OPTION] [FILE]...\n\
152 Print or check %s (%d-bit) checksums.\n\
153 With no FILE, or when FILE is -, read standard input.\n\
161 -b, --binary read in binary mode (default unless reading tty stdin)\n\
165 -b, --binary read in binary mode\n\
168 -c, --check read %s sums from the FILEs and check them\n"),
172 -t, --text read in text mode (default if reading tty stdin)\n\
176 -t, --text read in text mode (default)\n\
180 The following two options are useful only when verifying checksums:\n\
181 --status don't output anything, status code shows success\n\
182 -w, --warn warn about improperly formatted checksum lines\n\
185 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
186 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
189 The sums are computed as described in %s. When checking, the input\n\
190 should be a former output of this program. The default mode is to print\n\
191 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
192 text), and name for each FILE.\n"),
194 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
200 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
202 /* Split the checksum string S (of length S_LEN) from a BSD 'md5' or
203 'sha1' command into two parts: a hexadecimal digest, and the file
204 name. S is modified. Return true if successful. */
207 bsd_split_3 (char *s
, size_t s_len
, unsigned char **hex_digest
, char **file_name
)
213 /* Find end of filename. The BSD 'md5' and 'sha1' commands do not escape
214 filenames, so search backwards for the last ')'. */
216 while (i
&& s
[i
] != ')')
224 while (ISWHITE (s
[i
]))
232 while (ISWHITE (s
[i
]))
235 *hex_digest
= (unsigned char *) &s
[i
];
239 /* Split the string S (of length S_LEN) into three parts:
240 a hexadecimal digest, binary flag, and the file name.
241 S is modified. Return true if successful. */
244 split_3 (char *s
, size_t s_len
,
245 unsigned char **hex_digest
, int *binary
, char **file_name
)
248 bool escaped_filename
= false;
249 size_t algo_name_len
;
252 while (ISWHITE (s
[i
]))
255 /* Check for BSD-style checksum line. */
256 algo_name_len
= strlen (DIGEST_TYPE_STRING
);
257 if (strncmp (s
+ i
, DIGEST_TYPE_STRING
, algo_name_len
) == 0)
259 if (strncmp (s
+ i
+ algo_name_len
, " (", 2) == 0)
262 return bsd_split_3 (s
+ i
+ algo_name_len
+ 2,
263 s_len
- (i
+ algo_name_len
+ 2),
264 hex_digest
, file_name
);
268 /* Ignore this line if it is too short.
269 Each line must have at least `min_digest_line_length - 1' (or one more, if
270 the first is a backslash) more characters to contain correct message digest
272 if (s_len
- i
< min_digest_line_length
+ (s
[i
] == '\\'))
278 escaped_filename
= true;
280 *hex_digest
= (unsigned char *) &s
[i
];
282 /* The first field has to be the n-character hexadecimal
283 representation of the message digest. If it is not followed
284 immediately by a white space it's an error. */
285 i
+= digest_hex_bytes
;
291 if (s
[i
] != ' ' && s
[i
] != '*')
293 *binary
= (s
[i
++] == '*');
295 /* All characters between the type indicator and end of line are
296 significant -- that includes leading and trailing white space. */
299 if (escaped_filename
)
301 /* Translate each `\n' string in the file name to a NEWLINE,
302 and each `\\' string to a backslash. */
313 /* A valid line does not end with a backslash. */
326 /* Only `\' or `n' may follow a backslash. */
332 /* The file name may not contain a NUL. */
347 hex_digits (unsigned char const *s
)
358 /* An interface to the function, DIGEST_STREAM.
359 Operate on FILENAME (it may be "-").
361 *BINARY indicates whether the file is binary. BINARY < 0 means it
362 depends on whether binary mode makes any difference and the file is
363 a terminal; in that case, clear *BINARY if the file was treated as
364 text because it was a terminal.
366 Put the checksum in *BIN_RESULT, which must be properly aligned.
367 Return true if successful. */
370 digest_file (const char *filename
, int *binary
, unsigned char *bin_result
)
374 bool is_stdin
= STREQ (filename
, "-");
378 have_read_stdin
= true;
380 if (O_BINARY
&& *binary
)
383 *binary
= ! isatty (STDIN_FILENO
);
385 freopen (NULL
, "rb", stdin
);
390 fp
= fopen (filename
, (O_BINARY
&& *binary
? "rb" : "r"));
393 error (0, errno
, "%s", filename
);
398 err
= DIGEST_STREAM (fp
, bin_result
);
401 error (0, errno
, "%s", filename
);
407 if (!is_stdin
&& fclose (fp
) != 0)
409 error (0, errno
, "%s", filename
);
417 digest_check (const char *checkfile_name
)
419 FILE *checkfile_stream
;
420 uintmax_t n_properly_formatted_lines
= 0;
421 uintmax_t n_mismatched_checksums
= 0;
422 uintmax_t n_open_or_read_failures
= 0;
423 unsigned char bin_buffer_unaligned
[DIGEST_BIN_BYTES
+ DIGEST_ALIGN
];
424 /* Make sure bin_buffer is properly aligned. */
425 unsigned char *bin_buffer
= ptr_align (bin_buffer_unaligned
, DIGEST_ALIGN
);
426 uintmax_t line_number
;
428 size_t line_chars_allocated
;
429 bool is_stdin
= STREQ (checkfile_name
, "-");
433 have_read_stdin
= true;
434 checkfile_name
= _("standard input");
435 checkfile_stream
= stdin
;
439 checkfile_stream
= fopen (checkfile_name
, "r");
440 if (checkfile_stream
== NULL
)
442 error (0, errno
, "%s", checkfile_name
);
449 line_chars_allocated
= 0;
454 unsigned char *hex_digest
IF_LINT (= NULL
);
458 if (line_number
== 0)
459 error (EXIT_FAILURE
, 0, _("%s: too many checksum lines"),
462 line_length
= getline (&line
, &line_chars_allocated
, checkfile_stream
);
463 if (line_length
<= 0)
466 /* Ignore comment lines, which begin with a '#' character. */
470 /* Remove any trailing newline. */
471 if (line
[line_length
- 1] == '\n')
472 line
[--line_length
] = '\0';
474 if (! (split_3 (line
, line_length
, &hex_digest
, &binary
, &filename
)
475 && ! (is_stdin
&& STREQ (filename
, "-"))
476 && hex_digits (hex_digest
)))
482 ": improperly formatted %s checksum line"),
483 checkfile_name
, line_number
,
489 static const char bin2hex
[] = { '0', '1', '2', '3',
492 'c', 'd', 'e', 'f' };
495 ++n_properly_formatted_lines
;
497 ok
= digest_file (filename
, &binary
, bin_buffer
);
501 ++n_open_or_read_failures
;
504 printf (_("%s: FAILED open or read\n"), filename
);
510 size_t digest_bin_bytes
= digest_hex_bytes
/ 2;
512 /* Compare generated binary number with text representation
513 in check file. Ignore case of hex digits. */
514 for (cnt
= 0; cnt
< digest_bin_bytes
; ++cnt
)
516 if (tolower (hex_digest
[2 * cnt
])
517 != bin2hex
[bin_buffer
[cnt
] >> 4]
518 || (tolower (hex_digest
[2 * cnt
+ 1])
519 != (bin2hex
[bin_buffer
[cnt
] & 0xf])))
522 if (cnt
!= digest_bin_bytes
)
523 ++n_mismatched_checksums
;
527 printf ("%s: %s\n", filename
,
528 (cnt
!= digest_bin_bytes
? _("FAILED") : _("OK")));
534 while (!feof (checkfile_stream
) && !ferror (checkfile_stream
));
538 if (ferror (checkfile_stream
))
540 error (0, 0, _("%s: read error"), checkfile_name
);
544 if (!is_stdin
&& fclose (checkfile_stream
) != 0)
546 error (0, errno
, "%s", checkfile_name
);
550 if (n_properly_formatted_lines
== 0)
552 /* Warn if no tests are found. */
553 error (0, 0, _("%s: no properly formatted %s checksum lines found"),
554 checkfile_name
, DIGEST_TYPE_STRING
);
560 if (n_open_or_read_failures
!= 0)
562 ngettext ("WARNING: %" PRIuMAX
" of %" PRIuMAX
563 " listed file could not be read",
564 "WARNING: %" PRIuMAX
" of %" PRIuMAX
565 " listed files could not be read",
566 select_plural (n_properly_formatted_lines
)),
567 n_open_or_read_failures
, n_properly_formatted_lines
);
569 if (n_mismatched_checksums
!= 0)
571 uintmax_t n_computed_checksums
=
572 (n_properly_formatted_lines
- n_open_or_read_failures
);
574 ngettext ("WARNING: %" PRIuMAX
" of %" PRIuMAX
575 " computed checksum did NOT match",
576 "WARNING: %" PRIuMAX
" of %" PRIuMAX
577 " computed checksums did NOT match",
578 select_plural (n_computed_checksums
)),
579 n_mismatched_checksums
, n_computed_checksums
);
584 return (n_properly_formatted_lines
!= 0
585 && n_mismatched_checksums
== 0
586 && n_open_or_read_failures
== 0);
590 main (int argc
, char **argv
)
592 unsigned char bin_buffer_unaligned
[DIGEST_BIN_BYTES
+ DIGEST_ALIGN
];
593 /* Make sure bin_buffer is properly aligned. */
594 unsigned char *bin_buffer
= ptr_align (bin_buffer_unaligned
, DIGEST_ALIGN
);
595 bool do_check
= false;
600 /* Setting values of global variables. */
601 initialize_main (&argc
, &argv
);
602 program_name
= argv
[0];
603 setlocale (LC_ALL
, "");
604 bindtextdomain (PACKAGE
, LOCALEDIR
);
605 textdomain (PACKAGE
);
607 atexit (close_stdout
);
609 while ((opt
= getopt_long (argc
, argv
, "bctw", long_options
, NULL
)) != -1)
629 case_GETOPT_HELP_CHAR
;
630 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
632 usage (EXIT_FAILURE
);
635 min_digest_line_length
= MIN_DIGEST_LINE_LENGTH
;
636 digest_hex_bytes
= DIGEST_HEX_BYTES
;
638 if (0 <= binary
&& do_check
)
640 error (0, 0, _("the --binary and --text options are meaningless when "
641 "verifying checksums"));
642 usage (EXIT_FAILURE
);
645 if (status_only
& !do_check
)
648 _("the --status option is meaningful only when verifying checksums"));
649 usage (EXIT_FAILURE
);
652 if (warn
& !do_check
)
655 _("the --warn option is meaningful only when verifying checksums"));
656 usage (EXIT_FAILURE
);
659 if (!O_BINARY
&& binary
< 0)
665 for (; optind
< argc
; ++optind
)
667 char *file
= argv
[optind
];
670 ok
&= digest_check (file
);
673 int file_is_binary
= binary
;
675 if (! digest_file (file
, &file_is_binary
, bin_buffer
))
681 /* Output a leading backslash if the file name contains
682 a newline or backslash. */
683 if (strchr (file
, '\n') || strchr (file
, '\\'))
686 for (i
= 0; i
< (digest_hex_bytes
/ 2); ++i
)
687 printf ("%02x", bin_buffer
[i
]);
695 /* Translate each NEWLINE byte to the string, "\\n",
696 and each backslash to "\\\\". */
697 for (i
= 0; i
< strlen (file
); ++i
)
702 fputs ("\\n", stdout
);
706 fputs ("\\\\", stdout
);
719 if (have_read_stdin
&& fclose (stdin
) == EOF
)
720 error (EXIT_FAILURE
, errno
, _("standard input"));
722 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);