6 |
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
7 |
*/ |
*/ |
8 |
|
|
9 |
#include "busybox.h" |
#include "libbb.h" |
10 |
|
|
11 |
typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t; |
typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t; |
12 |
|
|
21 |
/* xzalloc zero-terminates */ |
/* xzalloc zero-terminates */ |
22 |
char *hex_value = xzalloc((hash_length * 2) + 1); |
char *hex_value = xzalloc((hash_length * 2) + 1); |
23 |
bin2hex(hex_value, (char*)hash_value, hash_length); |
bin2hex(hex_value, (char*)hash_value, hash_length); |
24 |
return hex_value; |
return (unsigned char *)hex_value; |
25 |
} |
} |
26 |
|
|
27 |
static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo) |
static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo) |
33 |
} context; |
} context; |
34 |
uint8_t *hash_value = NULL; |
uint8_t *hash_value = NULL; |
35 |
RESERVE_CONFIG_UBUFFER(in_buf, 4096); |
RESERVE_CONFIG_UBUFFER(in_buf, 4096); |
36 |
void (*update)(const void*, size_t, void*); |
void FAST_FUNC (*update)(const void*, size_t, void*); |
37 |
void (*final)(void*, void*); |
void FAST_FUNC (*final)(void*, void*); |
38 |
|
|
39 |
src_fd = STDIN_FILENO; |
src_fd = open_or_warn_stdin(filename); |
40 |
if (NOT_LONE_DASH(filename)) { |
if (src_fd < 0) { |
41 |
src_fd = open(filename, O_RDONLY); |
return NULL; |
|
if (src_fd < 0) { |
|
|
bb_perror_msg("%s", filename); |
|
|
return NULL; |
|
|
} |
|
42 |
} |
} |
43 |
|
|
44 |
/* figure specific hash algorithims */ |
/* figure specific hash algorithims */ |
45 |
if (ENABLE_MD5SUM && hash_algo==HASH_MD5) { |
if (ENABLE_MD5SUM && hash_algo==HASH_MD5) { |
46 |
md5_begin(&context.md5); |
md5_begin(&context.md5); |
47 |
update = (void (*)(const void*, size_t, void*))md5_hash; |
update = (void*)md5_hash; |
48 |
final = (void (*)(void*, void*))md5_end; |
final = (void*)md5_end; |
49 |
hash_len = 16; |
hash_len = 16; |
50 |
} else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) { |
} else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) { |
51 |
sha1_begin(&context.sha1); |
sha1_begin(&context.sha1); |
52 |
update = (void (*)(const void*, size_t, void*))sha1_hash; |
update = (void*)sha1_hash; |
53 |
final = (void (*)(void*, void*))sha1_end; |
final = (void*)sha1_end; |
54 |
hash_len = 20; |
hash_len = 20; |
55 |
} else { |
} else { |
56 |
bb_error_msg_and_die("algorithm not supported"); |
bb_error_msg_and_die("algorithm not supported"); |
74 |
return hash_value; |
return hash_value; |
75 |
} |
} |
76 |
|
|
77 |
int md5_sha1_sum_main(int argc, char **argv) |
int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
78 |
|
int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) |
79 |
{ |
{ |
80 |
int return_value = EXIT_SUCCESS; |
int return_value = EXIT_SUCCESS; |
81 |
uint8_t *hash_value; |
uint8_t *hash_value; |
82 |
unsigned flags; |
unsigned flags; |
83 |
hash_algo_t hash_algo = ENABLE_MD5SUM |
hash_algo_t hash_algo = ENABLE_MD5SUM |
84 |
? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5) |
? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5) |
85 |
: HASH_SHA1; |
: HASH_SHA1; |
86 |
|
|
87 |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) |
88 |
flags = getopt32(argc, argv, "scw"); |
flags = getopt32(argv, "scw"); |
89 |
else optind = 1; |
else optind = 1; |
90 |
|
argv += optind; |
91 |
|
//argc -= optind; |
92 |
|
if (!*argv) |
93 |
|
*--argv = (char*)"-"; |
94 |
|
|
95 |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { |
96 |
if (flags & FLAG_SILENT) { |
if (flags & FLAG_SILENT) { |
102 |
} |
} |
103 |
} |
} |
104 |
|
|
|
if (argc == optind) { |
|
|
argv[argc++] = "-"; |
|
|
} |
|
|
|
|
105 |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) { |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) { |
106 |
FILE *pre_computed_stream; |
FILE *pre_computed_stream; |
107 |
int count_total = 0; |
int count_total = 0; |
108 |
int count_failed = 0; |
int count_failed = 0; |
|
char *file_ptr = argv[optind]; |
|
109 |
char *line; |
char *line; |
110 |
|
|
111 |
if (optind + 1 != argc) { |
if (argv[1]) { |
112 |
bb_error_msg_and_die |
bb_error_msg_and_die |
113 |
("only one argument may be specified when using -c"); |
("only one argument may be specified when using -c"); |
114 |
} |
} |
115 |
|
|
116 |
pre_computed_stream = stdin; |
pre_computed_stream = xfopen_stdin(argv[0]); |
|
if (NOT_LONE_DASH(file_ptr)) { |
|
|
pre_computed_stream = xfopen(file_ptr, "r"); |
|
|
} |
|
117 |
|
|
118 |
while ((line = xmalloc_getline(pre_computed_stream)) != NULL) { |
while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) { |
119 |
char *filename_ptr; |
char *filename_ptr; |
120 |
|
|
121 |
count_total++; |
count_total++; |
161 |
} |
} |
162 |
*/ |
*/ |
163 |
} else { |
} else { |
164 |
while (optind < argc) { |
do { |
165 |
char *file_ptr = argv[optind++]; |
hash_value = hash_file(*argv, hash_algo); |
|
|
|
|
hash_value = hash_file(file_ptr, hash_algo); |
|
166 |
if (hash_value == NULL) { |
if (hash_value == NULL) { |
167 |
return_value = EXIT_FAILURE; |
return_value = EXIT_FAILURE; |
168 |
} else { |
} else { |
169 |
printf("%s %s\n", hash_value, file_ptr); |
printf("%s %s\n", hash_value, *argv); |
170 |
free(hash_value); |
free(hash_value); |
171 |
} |
} |
172 |
} |
} while (*++argv); |
173 |
} |
} |
174 |
return return_value; |
return return_value; |
175 |
} |
} |