Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/md5_sha1_sum.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (hide annotations) (download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 4855 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 niro 532 /* 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 niro 816 #include "libbb.h"
10 niro 532
11 niro 984 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 niro 532
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 niro 816 return (unsigned char *)hex_value;
31 niro 532 }
32    
33 niro 984 static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
34 niro 532 {
35     int src_fd, hash_len, count;
36     union _ctx_ {
37 niro 984 sha512_ctx_t sha512;
38     sha256_ctx_t sha256;
39 niro 532 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 niro 816 void FAST_FUNC (*update)(const void*, size_t, void*);
45     void FAST_FUNC (*final)(void*, void*);
46 niro 984 hash_algo_t hash_algo = applet_name[3];
47 niro 532
48 niro 816 src_fd = open_or_warn_stdin(filename);
49     if (src_fd < 0) {
50     return NULL;
51 niro 532 }
52    
53     /* figure specific hash algorithims */
54 niro 984 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
55 niro 532 md5_begin(&context.md5);
56 niro 816 update = (void*)md5_hash;
57     final = (void*)md5_end;
58 niro 532 hash_len = 16;
59 niro 984 } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
60 niro 532 sha1_begin(&context.sha1);
61 niro 816 update = (void*)sha1_hash;
62     final = (void*)sha1_end;
63 niro 532 hash_len = 20;
64 niro 984 } 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 niro 532 } 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 niro 816 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 niro 532 {
99     int return_value = EXIT_SUCCESS;
100     uint8_t *hash_value;
101     unsigned flags;
102 niro 984 /*hash_algo_t hash_algo = applet_name[3];*/
103 niro 532
104 niro 984 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
105     /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
106     flags = getopt32(argv, "scwbt");
107     }
108 niro 532 else optind = 1;
109 niro 816 argv += optind;
110     //argc -= optind;
111     if (!*argv)
112     *--argv = (char*)"-";
113 niro 532
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 niro 816 if (argv[1]) {
131 niro 532 bb_error_msg_and_die
132     ("only one argument may be specified when using -c");
133     }
134    
135 niro 816 pre_computed_stream = xfopen_stdin(argv[0]);
136 niro 532
137 niro 816 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
138 niro 532 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 niro 984 hash_value = hash_file(filename_ptr /*, hash_algo*/);
159 niro 532
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 niro 984 bb_perror_msg_and_die("can't close file %s", file_ptr);
180 niro 532 }
181     */
182     } else {
183 niro 816 do {
184 niro 984 hash_value = hash_file(*argv/*, hash_algo*/);
185 niro 532 if (hash_value == NULL) {
186     return_value = EXIT_FAILURE;
187     } else {
188 niro 816 printf("%s %s\n", hash_value, *argv);
189 niro 532 free(hash_value);
190     }
191 niro 816 } while (*++argv);
192 niro 532 }
193     return return_value;
194     }