aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/md5_sha1_sum.c53
1 files changed, 35 insertions, 18 deletions
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 9a243a26e..42a1d7acf 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -47,10 +47,43 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
47 return (hex_value); 47 return (hex_value);
48} 48}
49 49
50static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
51{
52 uint8_t *hash_value_bin;
53 uint8_t *hash_value = NULL;
54 uint8_t hash_length;
55 int src_fd;
56
57 if (strcmp(filename, "-") == 0) {
58 src_fd = fileno(stdin);
59 } else {
60 src_fd = open(filename, O_RDONLY);
61 }
62
63 if (hash_algo == HASH_MD5) {
64 hash_length = 16;
65 } else {
66 hash_length = 20;
67 }
68
69 hash_value_bin = xmalloc(hash_length);
70
71 if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
72 hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
73 } else {
74 bb_perror_msg("%s", filename);
75 }
76
77 close(src_fd);
78
79 return(hash_value);
80}
81
50/* This could become a common function for md5 as well, by using md5_stream */ 82/* This could become a common function for md5 as well, by using md5_stream */
51extern int hash_files(int argc, char **argv, const uint8_t hash_algo) 83extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
52{ 84{
53 int return_value = EXIT_SUCCESS; 85 int return_value = EXIT_SUCCESS;
86 uint8_t *hash_value;
54 87
55#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK 88#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
56 unsigned int flags; 89 unsigned int flags;
@@ -129,7 +162,6 @@ extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
129 } else 162 } else
130#endif 163#endif
131 { 164 {
132 uint8_t *hash_value_bin;
133 uint8_t hash_length; 165 uint8_t hash_length;
134 166
135 if (hash_algo == HASH_MD5) { 167 if (hash_algo == HASH_MD5) {
@@ -137,27 +169,12 @@ extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
137 } else { 169 } else {
138 hash_length = 20; 170 hash_length = 20;
139 } 171 }
140 hash_value_bin = xmalloc(hash_length); 172 hash_value = xmalloc(hash_length);
141 173
142 while (optind < argc) { 174 while (optind < argc) {
143 unsigned char *file_ptr = argv[optind++]; 175 unsigned char *file_ptr = argv[optind++];
144 uint8_t *hash_value;
145 int src_fd;
146
147 if ((file_ptr[0] == '-') && (file_ptr[1] == '\0')) {
148 src_fd = fileno(stdin);
149 } else {
150 src_fd = open(file_ptr, O_RDONLY);
151 }
152
153 if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
154 hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
155 } else {
156 bb_perror_msg("%s", file_ptr);
157 continue;
158 }
159 close(src_fd);
160 176
177 hash_value = hash_file(file_ptr, hash_algo);
161 if (hash_value == NULL) { 178 if (hash_value == NULL) {
162 return_value++; 179 return_value++;
163 } else { 180 } else {