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,
19 unsigned char hash_length)
22 unsigned char *hex_value;
24 max = (hash_length * 2) + 2;
25 hex_value = xmalloc(max);
26 for (x = len = 0; x < hash_length; x++) {
27 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
32 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
34 int src_fd, hash_len, count;
39 uint8_t *hash_value = NULL;
40 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
41 void (*update)(const void*, size_t, void*);
42 void (*final)(void*, void*);
44 if (strcmp(filename, "-") == 0) {
45 src_fd = STDIN_FILENO;
46 } else if(0 > (src_fd = open(filename, O_RDONLY))) {
47 bb_perror_msg("%s", filename);
51 /* figure specific hash algorithims */
52 if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
53 md5_begin(&context.md5);
54 update = (void (*)(const void*, size_t, void*))md5_hash;
55 final = (void (*)(void*, void*))md5_end;
57 } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
58 sha1_begin(&context.sha1);
59 update = (void (*)(const void*, size_t, void*))sha1_hash;
60 final = (void (*)(void*, void*))sha1_end;
63 bb_error_msg_and_die("algorithm not supported");
66 while (0 < (count = read(src_fd, in_buf, 4096))) {
67 update(in_buf, count, &context);
71 final(in_buf, &context);
72 hash_value = hash_bin_to_hex(in_buf, hash_len);
75 RELEASE_CONFIG_BUFFER(in_buf);
77 if (src_fd != STDIN_FILENO) {
84 int md5_sha1_sum_main(int argc, char **argv)
86 int return_value = EXIT_SUCCESS;
89 hash_algo_t hash_algo = ENABLE_MD5SUM
90 ? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
93 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
94 flags = getopt32(argc, argv, "scw");
97 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
98 if (flags & FLAG_SILENT) {
100 ("-%c is meaningful only when verifying checksums", 's');
101 } else if (flags & FLAG_WARN) {
103 ("-%c is meaningful only when verifying checksums", 'w');
107 if (argc == optind) {
111 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
112 FILE *pre_computed_stream;
114 int count_failed = 0;
115 char *file_ptr = argv[optind];
118 if (optind + 1 != argc) {
120 ("only one argument may be specified when using -c");
123 if (strcmp(file_ptr, "-") == 0) {
124 pre_computed_stream = stdin;
126 pre_computed_stream = xfopen(file_ptr, "r");
129 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
133 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);
165 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
166 bb_perror_msg_and_die("cannot close file %s", file_ptr);
169 while (optind < argc) {
170 char *file_ptr = argv[optind++];
172 hash_value = hash_file(file_ptr, hash_algo);
173 if (hash_value == NULL) {
174 return_value = EXIT_FAILURE;
176 printf("%s %s\n", hash_value, file_ptr);