8 |
|
|
9 |
#include "libbb.h" |
#include "libbb.h" |
10 |
|
|
11 |
typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t; |
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 |
#define FLAG_SILENT 1 |
20 |
#define FLAG_CHECK 2 |
#define FLAG_CHECK 2 |
30 |
return (unsigned char *)hex_value; |
return (unsigned char *)hex_value; |
31 |
} |
} |
32 |
|
|
33 |
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*/) |
34 |
{ |
{ |
35 |
int src_fd, hash_len, count; |
int src_fd, hash_len, count; |
36 |
union _ctx_ { |
union _ctx_ { |
37 |
|
sha512_ctx_t sha512; |
38 |
|
sha256_ctx_t sha256; |
39 |
sha1_ctx_t sha1; |
sha1_ctx_t sha1; |
40 |
md5_ctx_t md5; |
md5_ctx_t md5; |
41 |
} context; |
} context; |
43 |
RESERVE_CONFIG_UBUFFER(in_buf, 4096); |
RESERVE_CONFIG_UBUFFER(in_buf, 4096); |
44 |
void FAST_FUNC (*update)(const void*, size_t, void*); |
void FAST_FUNC (*update)(const void*, size_t, void*); |
45 |
void FAST_FUNC (*final)(void*, void*); |
void FAST_FUNC (*final)(void*, void*); |
46 |
|
hash_algo_t hash_algo = applet_name[3]; |
47 |
|
|
48 |
src_fd = open_or_warn_stdin(filename); |
src_fd = open_or_warn_stdin(filename); |
49 |
if (src_fd < 0) { |
if (src_fd < 0) { |
51 |
} |
} |
52 |
|
|
53 |
/* figure specific hash algorithims */ |
/* figure specific hash algorithims */ |
54 |
if (ENABLE_MD5SUM && hash_algo==HASH_MD5) { |
if (ENABLE_MD5SUM && hash_algo == HASH_MD5) { |
55 |
md5_begin(&context.md5); |
md5_begin(&context.md5); |
56 |
update = (void*)md5_hash; |
update = (void*)md5_hash; |
57 |
final = (void*)md5_end; |
final = (void*)md5_end; |
58 |
hash_len = 16; |
hash_len = 16; |
59 |
} else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) { |
} else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) { |
60 |
sha1_begin(&context.sha1); |
sha1_begin(&context.sha1); |
61 |
update = (void*)sha1_hash; |
update = (void*)sha1_hash; |
62 |
final = (void*)sha1_end; |
final = (void*)sha1_end; |
63 |
hash_len = 20; |
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 { |
} else { |
75 |
bb_error_msg_and_die("algorithm not supported"); |
bb_error_msg_and_die("algorithm not supported"); |
76 |
} |
} |
99 |
int return_value = EXIT_SUCCESS; |
int return_value = EXIT_SUCCESS; |
100 |
uint8_t *hash_value; |
uint8_t *hash_value; |
101 |
unsigned flags; |
unsigned flags; |
102 |
hash_algo_t hash_algo = ENABLE_MD5SUM |
/*hash_algo_t hash_algo = applet_name[3];*/ |
|
? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5) |
|
|
: HASH_SHA1; |
|
103 |
|
|
104 |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) |
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) { |
105 |
flags = getopt32(argv, "scw"); |
/* -b "binary", -t "text" are ignored (shaNNNsum compat) */ |
106 |
|
flags = getopt32(argv, "scwbt"); |
107 |
|
} |
108 |
else optind = 1; |
else optind = 1; |
109 |
argv += optind; |
argv += optind; |
110 |
//argc -= optind; |
//argc -= optind; |
155 |
*filename_ptr = '\0'; |
*filename_ptr = '\0'; |
156 |
filename_ptr += 2; |
filename_ptr += 2; |
157 |
|
|
158 |
hash_value = hash_file(filename_ptr, hash_algo); |
hash_value = hash_file(filename_ptr /*, hash_algo*/); |
159 |
|
|
160 |
if (hash_value && (strcmp((char*)hash_value, line) == 0)) { |
if (hash_value && (strcmp((char*)hash_value, line) == 0)) { |
161 |
if (!(flags & FLAG_SILENT)) |
if (!(flags & FLAG_SILENT)) |
176 |
} |
} |
177 |
/* |
/* |
178 |
if (fclose_if_not_stdin(pre_computed_stream) == EOF) { |
if (fclose_if_not_stdin(pre_computed_stream) == EOF) { |
179 |
bb_perror_msg_and_die("cannot close file %s", file_ptr); |
bb_perror_msg_and_die("can't close file %s", file_ptr); |
180 |
} |
} |
181 |
*/ |
*/ |
182 |
} else { |
} else { |
183 |
do { |
do { |
184 |
hash_value = hash_file(*argv, hash_algo); |
hash_value = hash_file(*argv/*, hash_algo*/); |
185 |
if (hash_value == NULL) { |
if (hash_value == NULL) { |
186 |
return_value = EXIT_FAILURE; |
return_value = EXIT_FAILURE; |
187 |
} else { |
} else { |