3d50bb0f5caec8f66ca099c7cf59d98f06533fbd
[external/busybox.git] / util-linux / coreutils / md5_sha1_sum.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Copyright (C) 2003 Glenn L. McGrath
4  *  Copyright (C) 2003-2004 Erik Andersen
5  *
6  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7  */
8
9 #include "libbb.h"
10
11 typedef enum {
12         /* 4th letter of applet_name is... */
13         HASH_MD5 = 's', /* "md5>s<um" */
14         HASH_SHA1 = '1',
15         HASH_SHA256 = '2',
16         HASH_SHA512 = '5',
17 } hash_algo_t;
18
19 #define FLAG_SILENT     1
20 #define FLAG_CHECK      2
21 #define FLAG_WARN       4
22
23 /* This might be useful elsewhere */
24 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
25                                 unsigned hash_length)
26 {
27         /* xzalloc zero-terminates */
28         char *hex_value = xzalloc((hash_length * 2) + 1);
29         bin2hex(hex_value, (char*)hash_value, hash_length);
30         return (unsigned char *)hex_value;
31 }
32
33 static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
34 {
35         int src_fd, hash_len, count;
36         union _ctx_ {
37                 sha512_ctx_t sha512;
38                 sha256_ctx_t sha256;
39                 sha1_ctx_t sha1;
40                 md5_ctx_t md5;
41         } context;
42         uint8_t *hash_value = NULL;
43         RESERVE_CONFIG_UBUFFER(in_buf, 4096);
44         void FAST_FUNC (*update)(const void*, size_t, void*);
45         void FAST_FUNC (*final)(void*, void*);
46         hash_algo_t hash_algo = applet_name[3];
47
48         src_fd = open_or_warn_stdin(filename);
49         if (src_fd < 0) {
50                 return NULL;
51         }
52
53         /* figure specific hash algorithims */
54         if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
55                 md5_begin(&context.md5);
56                 update = (void*)md5_hash;
57                 final = (void*)md5_end;
58                 hash_len = 16;
59         } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
60                 sha1_begin(&context.sha1);
61                 update = (void*)sha1_hash;
62                 final = (void*)sha1_end;
63                 hash_len = 20;
64         } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
65                 sha256_begin(&context.sha256);
66                 update = (void*)sha256_hash;
67                 final = (void*)sha256_end;
68                 hash_len = 32;
69         } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
70                 sha512_begin(&context.sha512);
71                 update = (void*)sha512_hash;
72                 final = (void*)sha512_end;
73                 hash_len = 64;
74         } else {
75                 bb_error_msg_and_die("algorithm not supported");
76         }
77
78         while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
79                 update(in_buf, count, &context);
80         }
81
82         if (count == 0) {
83                 final(in_buf, &context);
84                 hash_value = hash_bin_to_hex(in_buf, hash_len);
85         }
86
87         RELEASE_CONFIG_BUFFER(in_buf);
88
89         if (src_fd != STDIN_FILENO) {
90                 close(src_fd);
91         }
92
93         return hash_value;
94 }
95
96 int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
97 int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
98 {
99         int return_value = EXIT_SUCCESS;
100         uint8_t *hash_value;
101         unsigned flags;
102         /*hash_algo_t hash_algo = applet_name[3];*/
103
104         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
105                 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
106                 flags = getopt32(argv, "scwbt");
107         }
108         else optind = 1;
109         argv += optind;
110         //argc -= optind;
111         if (!*argv)
112                 *--argv = (char*)"-";
113
114         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
115                 if (flags & FLAG_SILENT) {
116                         bb_error_msg_and_die
117                                 ("-%c is meaningful only when verifying checksums", 's');
118                 } else if (flags & FLAG_WARN) {
119                         bb_error_msg_and_die
120                                 ("-%c is meaningful only when verifying checksums", 'w');
121                 }
122         }
123
124         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
125                 FILE *pre_computed_stream;
126                 int count_total = 0;
127                 int count_failed = 0;
128                 char *line;
129
130                 if (argv[1]) {
131                         bb_error_msg_and_die
132                                 ("only one argument may be specified when using -c");
133                 }
134
135                 pre_computed_stream = xfopen_stdin(argv[0]);
136
137                 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
138                         char *filename_ptr;
139
140                         count_total++;
141                         filename_ptr = strstr(line, "  ");
142                         /* handle format for binary checksums */
143                         if (filename_ptr == NULL) {
144                                 filename_ptr = strstr(line, " *");
145                         }
146                         if (filename_ptr == NULL) {
147                                 if (flags & FLAG_WARN) {
148                                         bb_error_msg("invalid format");
149                                 }
150                                 count_failed++;
151                                 return_value = EXIT_FAILURE;
152                                 free(line);
153                                 continue;
154                         }
155                         *filename_ptr = '\0';
156                         filename_ptr += 2;
157
158                         hash_value = hash_file(filename_ptr /*, hash_algo*/);
159
160                         if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
161                                 if (!(flags & FLAG_SILENT))
162                                         printf("%s: OK\n", filename_ptr);
163                         } else {
164                                 if (!(flags & FLAG_SILENT))
165                                         printf("%s: FAILED\n", filename_ptr);
166                                 count_failed++;
167                                 return_value = EXIT_FAILURE;
168                         }
169                         /* possible free(NULL) */
170                         free(hash_value);
171                         free(line);
172                 }
173                 if (count_failed && !(flags & FLAG_SILENT)) {
174                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
175                                                  count_failed, count_total);
176                 }
177                 /*
178                 if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
179                         bb_perror_msg_and_die("can't close file %s", file_ptr);
180                 }
181                 */
182         } else {
183                 do {
184                         hash_value = hash_file(*argv/*, hash_algo*/);
185                         if (hash_value == NULL) {
186                                 return_value = EXIT_FAILURE;
187                         } else {
188                                 printf("%s  %s\n", hash_value, *argv);
189                                 free(hash_value);
190                         }
191                 } while (*++argv);
192         }
193         return return_value;
194 }