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.
19 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
25 /* This might be useful elsewhere */
26 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
27 unsigned char hash_length)
30 unsigned char *hex_value;
32 max = (hash_length * 2) + 2;
33 hex_value = xmalloc(max);
34 for (x = len = 0; x < hash_length; x++) {
35 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
40 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
42 int src_fd, hash_len, count;
47 uint8_t *hash_value = NULL;
48 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
49 void (*update)(const void*, size_t, void*);
50 void (*final)(void*, void*);
52 if (strcmp(filename, "-") == 0) {
53 src_fd = STDIN_FILENO;
54 } else if(0 > (src_fd = open(filename, O_RDONLY))) {
55 bb_perror_msg("%s", filename);
59 /* figure specific hash algorithims */
60 if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
61 md5_begin(&context.md5);
62 update = (void (*)(const void*, size_t, void*))md5_hash;
63 final = (void (*)(void*, void*))md5_end;
65 } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
66 sha1_begin(&context.sha1);
67 update = (void (*)(const void*, size_t, void*))sha1_hash;
68 final = (void (*)(void*, void*))sha1_end;
71 bb_error_msg_and_die("algorithm not supported");
74 while (0 < (count = read(src_fd, in_buf, 4096))) {
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));