add hashsum multi-hash option

This commit is contained in:
Steffen Jaeckel 2017-07-05 14:26:53 +02:00
parent ebc9d2943b
commit e1fcd3ab04

View File

@ -159,7 +159,7 @@ ERR:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int idx, check, z, err, argn; int idxs[TAB_SIZE], idx, check, y, z, err, argn;
unsigned long w, x; unsigned long w, x;
unsigned char hash_buffer[MAXBLOCKSIZE]; unsigned char hash_buffer[MAXBLOCKSIZE];
@ -175,16 +175,19 @@ int main(int argc, char **argv)
die(EXIT_FAILURE); die(EXIT_FAILURE);
} }
for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
idxs[x] = -2;
}
argn = 1; argn = 1;
check = 0; check = 0;
idx = -2; idx = 0;
while(argn < argc){ while(argn < argc){
if(strcmp("-a", argv[argn]) == 0) { if(strcmp("-a", argv[argn]) == 0) {
argn++; argn++;
if(argn < argc) { if(argn < argc) {
idx = find_hash(argv[argn]); idxs[idx] = find_hash(argv[argn]);
if (idx == -1) { if (idxs[idx] == -1) {
struct { struct {
const char* is; const char* is;
const char* should; const char* should;
@ -215,15 +218,20 @@ int main(int argc, char **argv)
}; };
for (x = 0; shasum_compat[x].is != NULL; ++x) { for (x = 0; shasum_compat[x].is != NULL; ++x) {
if(XSTRCMP(shasum_compat[x].is, argv[argn]) == 0) { if(XSTRCMP(shasum_compat[x].is, argv[argn]) == 0) {
idx = find_hash(shasum_compat[x].should); idxs[idx] = find_hash(shasum_compat[x].should);
break; break;
} }
} }
} }
if (idx == -1) { if (idxs[idx] == -1) {
fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum); fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum);
die(EXIT_FAILURE); die(EXIT_FAILURE);
} }
idx++;
if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
die(EXIT_FAILURE);
}
argn++; argn++;
continue; continue;
} }
@ -245,7 +253,7 @@ int main(int argc, char **argv)
if (argc == argn) { if (argc == argn) {
w = sizeof(hash_buffer); w = sizeof(hash_buffer);
if ((err = hash_filehandle(idx, stdin, hash_buffer, &w)) != CRYPT_OK) { if ((err = hash_filehandle(idxs[0], stdin, hash_buffer, &w)) != CRYPT_OK) {
fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err)); fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
return EXIT_FAILURE; return EXIT_FAILURE;
} else { } else {
@ -255,14 +263,16 @@ int main(int argc, char **argv)
printf(" *-\n"); printf(" *-\n");
} }
} else { } else {
for (z = 3; z < argc; z++) { for (z = argn; z < argc; z++) {
w = sizeof(hash_buffer); for (y = 0; y < idx; ++y) {
if ((err = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) { w = sizeof(hash_buffer);
fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err)); if ((err = hash_file(idxs[y],argv[z],hash_buffer,&w)) != CRYPT_OK) {
return EXIT_FAILURE; fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
} else { return EXIT_FAILURE;
printf_hex(hash_buffer, w); } else {
printf(" *%s\n", argv[z]); printf_hex(hash_buffer, w);
printf(" *%s\n", argv[z]);
}
} }
} }
} }