1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2003 Glenn L. McGrath
4 * Copyright (C) 2003-2004 Erik Andersen
6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
17 /* This might be useful elsewhere */
18 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
21 /* xzalloc zero-terminates */
22 char *hex_value = xzalloc((hash_length * 2) + 1);
23 bin2hex(hex_value, (char*)hash_value, hash_length);
27 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
29 int src_fd, hash_len, count;
34 uint8_t *hash_value = NULL;
35 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
36 void (*update)(const void*, size_t, void*);
37 void (*final)(void*, void*);
39 src_fd = STDIN_FILENO;
40 if (NOT_LONE_DASH(filename)) {
41 src_fd = open_or_warn(filename, O_RDONLY);
47 /* figure specific hash algorithims */
48 if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
49 md5_begin(&context.md5);
50 update = (void (*)(const void*, size_t, void*))md5_hash;
51 final = (void (*)(void*, void*))md5_end;
53 } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
54 sha1_begin(&context.sha1);
55 update = (void (*)(const void*, size_t, void*))sha1_hash;
56 final = (void (*)(void*, void*))sha1_end;
59 bb_error_msg_and_die("algorithm not supported");
62 while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
63 update(in_buf, count, &context);
67 final(in_buf, &context);
68 hash_value = hash_bin_to_hex(in_buf, hash_len);
71 RELEASE_CONFIG_BUFFER(in_buf);
73 if (src_fd != STDIN_FILENO) {
80 int md5_sha1_sum_main(int argc, char **argv);
81 int md5_sha1_sum_main(int argc, char **argv)
83 int return_value = EXIT_SUCCESS;
86 hash_algo_t hash_algo = ENABLE_MD5SUM
87 ? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
90 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
91 flags = getopt32(argv, "scw");
94 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
95 if (flags & FLAG_SILENT) {
97 ("-%c is meaningful only when verifying checksums", 's');
98 } else if (flags & FLAG_WARN) {
100 ("-%c is meaningful only when verifying checksums", 'w');
104 if (argc == optind) {
105 argv[argc++] = (char*)"-";
108 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
109 FILE *pre_computed_stream;
111 int count_failed = 0;
112 char *file_ptr = argv[optind];
115 if (optind + 1 != argc) {
117 ("only one argument may be specified when using -c");
120 pre_computed_stream = stdin;
121 if (NOT_LONE_DASH(file_ptr)) {
122 pre_computed_stream = xfopen(file_ptr, "r");
125 while ((line = xmalloc_getline(pre_computed_stream)) != NULL) {
129 filename_ptr = strstr(line, " ");
130 /* handle format for binary checksums */
131 if (filename_ptr == NULL) {
132 filename_ptr = strstr(line, " *");
134 if (filename_ptr == NULL) {
135 if (flags & FLAG_WARN) {
136 bb_error_msg("invalid format");
139 return_value = EXIT_FAILURE;
143 *filename_ptr = '\0';
146 hash_value = hash_file(filename_ptr, hash_algo);
148 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
149 if (!(flags & FLAG_SILENT))
150 printf("%s: OK\n", filename_ptr);
152 if (!(flags & FLAG_SILENT))
153 printf("%s: FAILED\n", filename_ptr);
155 return_value = EXIT_FAILURE;
157 /* possible free(NULL) */
161 if (count_failed && !(flags & FLAG_SILENT)) {
162 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
163 count_failed, count_total);
166 if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
167 bb_perror_msg_and_die("cannot close file %s", file_ptr);
171 while (optind < argc) {
172 char *file_ptr = argv[optind++];
174 hash_value = hash_file(file_ptr, hash_algo);
175 if (hash_value == NULL) {
176 return_value = EXIT_FAILURE;
178 printf("%s %s\n", hash_value, file_ptr);