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.
23 /* This might be useful elsewhere */
24 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
25 unsigned char hash_length)
28 unsigned char *hex_value;
30 max = (hash_length * 2) + 2;
31 hex_value = xmalloc(max);
32 for (x = len = 0; x < hash_length; x++) {
33 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
38 static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
40 int src_fd = strcmp(filename, "-") == 0 ? STDIN_FILENO :
41 open(filename, O_RDONLY);
43 bb_perror_msg("%s", filename);
47 RESERVE_CONFIG_UBUFFER(hash_value_bin, 20);
48 hash_value = hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2 ?
49 hash_bin_to_hex(hash_value_bin, hash_algo == HASH_MD5 ? 16 : 20) :
51 RELEASE_CONFIG_BUFFER(hash_value_bin);
57 /* This could become a common function for md5 as well, by using md5_stream */
58 static int hash_files(int argc, char **argv, const uint8_t hash_algo)
60 int return_value = EXIT_SUCCESS;
63 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
66 flags = bb_getopt_ulflags(argc, argv, "scw");
69 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
70 if (!(flags & FLAG_CHECK)) {
71 if (flags & FLAG_SILENT) {
73 ("the -s option is meaningful only when verifying checksums");
74 } else if (flags & FLAG_WARN) {
76 ("the -w option is meaningful only when verifying checksums");
84 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
85 if (flags & FLAG_CHECK) {
86 FILE *pre_computed_stream;
89 char *file_ptr = argv[optind];
92 if (optind + 1 != argc) {
94 ("only one argument may be specified when using -c");
97 if (strcmp(file_ptr, "-") == 0) {
98 pre_computed_stream = stdin;
100 pre_computed_stream = bb_xfopen(file_ptr, "r");
103 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
107 filename_ptr = strstr(line, " ");
108 if (filename_ptr == NULL) {
109 if (flags & FLAG_WARN) {
110 bb_error_msg("Invalid format");
113 return_value = EXIT_FAILURE;
117 *filename_ptr = '\0';
120 hash_value = hash_file(filename_ptr, hash_algo);
122 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
123 if (!(flags & FLAG_SILENT))
124 printf("%s: OK\n", filename_ptr);
126 if (!(flags & FLAG_SILENT))
127 printf("%s: FAILED\n", filename_ptr);
129 return_value = EXIT_FAILURE;
131 /* possible free(NULL) */
135 if (count_failed && !(flags & FLAG_SILENT)) {
136 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
137 count_failed, count_total);
139 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
140 bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
145 while (optind < argc) {
146 char *file_ptr = argv[optind++];
148 hash_value = hash_file(file_ptr, hash_algo);
149 if (hash_value == NULL) {
150 return_value = EXIT_FAILURE;
152 printf("%s %s\n", hash_value, file_ptr);
157 return (return_value);
161 int md5sum_main(int argc, char **argv)
163 return(hash_files(argc, argv, HASH_MD5));
167 #ifdef CONFIG_SHA1SUM
168 int sha1sum_main(int argc, char **argv)
170 return(hash_files(argc, argv, HASH_SHA1));