1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2003 Glenn L. McGrath
4 * Copyright (C) 2003-2004 Erik Andersen
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 /* This is a NOEXEC applet. Be very careful! */
14 /* 4th letter of applet_name is... */
15 HASH_MD5 = 's', /* "md5>s<um" */
25 /* This might be useful elsewhere */
26 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
29 /* xzalloc zero-terminates */
30 char *hex_value = xzalloc((hash_length * 2) + 1);
31 bin2hex(hex_value, (char*)hash_value, hash_length);
32 return (unsigned char *)hex_value;
35 static uint8_t *hash_file(const char *filename)
37 int src_fd, hash_len, count;
44 uint8_t *hash_value = NULL;
45 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
46 void FAST_FUNC (*update)(void*, const void*, size_t);
47 void FAST_FUNC (*final)(void*, void*);
50 src_fd = open_or_warn_stdin(filename);
55 hash_algo = applet_name[3];
57 /* figure specific hash algorithims */
58 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
59 md5_begin(&context.md5);
60 update = (void*)md5_hash;
61 final = (void*)md5_end;
63 } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
64 sha1_begin(&context.sha1);
65 update = (void*)sha1_hash;
66 final = (void*)sha1_end;
68 } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
69 sha256_begin(&context.sha256);
70 update = (void*)sha256_hash;
71 final = (void*)sha256_end;
73 } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
74 sha512_begin(&context.sha512);
75 update = (void*)sha512_hash;
76 final = (void*)sha512_end;
79 xfunc_die(); /* can't reach this */
82 while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
83 update(&context, in_buf, count);
87 final(&context, in_buf);
88 hash_value = hash_bin_to_hex(in_buf, hash_len);
91 RELEASE_CONFIG_BUFFER(in_buf);
93 if (src_fd != STDIN_FILENO) {
100 int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
101 int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
103 int return_value = EXIT_SUCCESS;
106 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
107 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
108 flags = getopt32(argv, "scwbt");
116 *--argv = (char*)"-";
118 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
119 if (flags & FLAG_SILENT) {
120 bb_error_msg_and_die("-%c is meaningful only with -c", 's');
122 if (flags & FLAG_WARN) {
123 bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
127 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
128 FILE *pre_computed_stream;
130 int count_failed = 0;
134 bb_error_msg_and_die("only one argument may be specified with -c");
137 pre_computed_stream = xfopen_stdin(argv[0]);
139 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
144 filename_ptr = strstr(line, " ");
145 /* handle format for binary checksums */
146 if (filename_ptr == NULL) {
147 filename_ptr = strstr(line, " *");
149 if (filename_ptr == NULL) {
150 if (flags & FLAG_WARN) {
151 bb_error_msg("invalid format");
154 return_value = EXIT_FAILURE;
158 *filename_ptr = '\0';
161 hash_value = hash_file(filename_ptr);
163 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
164 if (!(flags & FLAG_SILENT))
165 printf("%s: OK\n", filename_ptr);
167 if (!(flags & FLAG_SILENT))
168 printf("%s: FAILED\n", filename_ptr);
170 return_value = EXIT_FAILURE;
172 /* possible free(NULL) */
176 if (count_failed && !(flags & FLAG_SILENT)) {
177 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
178 count_failed, count_total);
181 if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
182 bb_perror_msg_and_die("can't close file %s", file_ptr);
187 uint8_t *hash_value = hash_file(*argv);
188 if (hash_value == NULL) {
189 return_value = EXIT_FAILURE;
191 printf("%s %s\n", hash_value, *argv);