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 ?
91 (**argv=='m' ? HASH_MD5 : HASH_SHA1)
95 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
96 flags = bb_getopt_ulflags(argc, argv, "scw");
99 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
100 if (flags & FLAG_SILENT) {
102 ("the -s option is meaningful only when verifying checksums");
103 } else if (flags & FLAG_WARN) {
105 ("the -w option is meaningful only when verifying checksums");
109 if (argc == optind) {
113 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
114 FILE *pre_computed_stream;
116 int count_failed = 0;
117 char *file_ptr = argv[optind];
120 if (optind + 1 != argc) {
122 ("only one argument may be specified when using -c");
125 if (strcmp(file_ptr, "-") == 0) {
126 pre_computed_stream = stdin;
128 pre_computed_stream = xfopen(file_ptr, "r");
131 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
135 filename_ptr = strstr(line, " ");
136 if (filename_ptr == NULL) {
137 if (flags & FLAG_WARN) {
138 bb_error_msg("Invalid format");
141 return_value = EXIT_FAILURE;
145 *filename_ptr = '\0';
148 hash_value = hash_file(filename_ptr, hash_algo);
150 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
151 if (!(flags & FLAG_SILENT))
152 printf("%s: OK\n", filename_ptr);
154 if (!(flags & FLAG_SILENT))
155 printf("%s: FAILED\n", filename_ptr);
157 return_value = EXIT_FAILURE;
159 /* possible free(NULL) */
163 if (count_failed && !(flags & FLAG_SILENT)) {
164 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
165 count_failed, count_total);
167 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
168 bb_perror_msg_and_die("Couldnt 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);
183 return (return_value);