2 * Copyright (C) 2003 Glenn L. McGrath
3 * Copyright (C) 2003-2004 Erik Andersen
5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
18 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
24 /* This might be useful elsewhere */
25 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
26 unsigned char hash_length)
29 unsigned char *hex_value;
31 max = (hash_length * 2) + 2;
32 hex_value = xmalloc(max);
33 for (x = len = 0; x < hash_length; x++) {
34 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
39 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
41 int src_fd, hash_len, count;
46 uint8_t *hash_value = NULL;
47 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
48 void (*update)(const void*, size_t, void*);
49 void (*final)(void*, void*);
51 if(strcmp(filename, "-") == 0) {
52 src_fd = STDIN_FILENO;
53 } else if(0 > (src_fd = open(filename, O_RDONLY))) {
54 bb_perror_msg("%s", filename);
58 // figure specific hash algorithims
59 if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
60 md5_begin(&context.md5);
61 update = (void (*)(const void*, size_t, void*))md5_hash;
62 final = (void (*)(void*, void*))md5_end;
64 } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
65 sha1_begin(&context.sha1);
66 update = (void (*)(const void*, size_t, void*))sha1_hash;
67 final = (void (*)(void*, void*))sha1_end;
70 bb_error_msg_and_die("algotithm not supported");
74 while(0 < (count = read(src_fd, in_buf, sizeof in_buf))) {
75 update(in_buf, count, &context);
79 final(in_buf, &context);
80 hash_value = hash_bin_to_hex(in_buf, hash_len);
83 RELEASE_CONFIG_BUFFER(in_buf);
85 if(src_fd != STDIN_FILENO) {
92 /* This could become a common function for md5 as well, by using md5_stream */
93 static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
95 int return_value = EXIT_SUCCESS;
99 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
100 flags = bb_getopt_ulflags(argc, argv, "scw");
102 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
103 if (flags & FLAG_SILENT) {
105 ("the -s option is meaningful only when verifying checksums");
106 } else if (flags & FLAG_WARN) {
108 ("the -w option is meaningful only when verifying checksums");
112 if (argc == optind) {
116 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
117 FILE *pre_computed_stream;
119 int count_failed = 0;
120 char *file_ptr = argv[optind];
123 if (optind + 1 != argc) {
125 ("only one argument may be specified when using -c");
128 if (strcmp(file_ptr, "-") == 0) {
129 pre_computed_stream = stdin;
131 pre_computed_stream = bb_xfopen(file_ptr, "r");
134 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
138 filename_ptr = strstr(line, " ");
139 if (filename_ptr == NULL) {
140 if (flags & FLAG_WARN) {
141 bb_error_msg("Invalid format");
144 return_value = EXIT_FAILURE;
148 *filename_ptr = '\0';
151 hash_value = hash_file(filename_ptr, hash_algo);
153 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
154 if (!(flags & FLAG_SILENT))
155 printf("%s: OK\n", filename_ptr);
157 if (!(flags & FLAG_SILENT))
158 printf("%s: FAILED\n", filename_ptr);
160 return_value = EXIT_FAILURE;
162 /* possible free(NULL) */
166 if (count_failed && !(flags & FLAG_SILENT)) {
167 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
168 count_failed, count_total);
170 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
171 bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
174 while (optind < argc) {
175 char *file_ptr = argv[optind++];
177 hash_value = hash_file(file_ptr, hash_algo);
178 if (hash_value == NULL) {
179 return_value = EXIT_FAILURE;
181 printf("%s %s\n", hash_value, file_ptr);
186 return (return_value);
190 int md5sum_main(int argc, char **argv)
192 return(hash_files(argc, argv, HASH_MD5));
196 #ifdef CONFIG_SHA1SUM
197 int sha1sum_main(int argc, char **argv)
199 return(hash_files(argc, argv, HASH_SHA1));