Skip to content

Commit

Permalink
add hashsum multi-hash option
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaeckel committed Jul 5, 2017
1 parent ebc9d29 commit e1fcd3a
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions demos/hashsum.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static void check_file(int argn, 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 char hash_buffer[MAXBLOCKSIZE];

Expand All @@ -175,16 +175,19 @@ int main(int argc, char **argv)
die(EXIT_FAILURE);
}

for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
idxs[x] = -2;
}
argn = 1;
check = 0;
idx = -2;
idx = 0;

while(argn < argc){
if(strcmp("-a", argv[argn]) == 0) {
argn++;
if(argn < argc) {
idx = find_hash(argv[argn]);
if (idx == -1) {
idxs[idx] = find_hash(argv[argn]);
if (idxs[idx] == -1) {
struct {
const char* is;
const char* should;
Expand Down Expand Up @@ -215,15 +218,20 @@ int main(int argc, char **argv)
};
for (x = 0; shasum_compat[x].is != NULL; ++x) {
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;
}
}
}
if (idx == -1) {
if (idxs[idx] == -1) {
fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum);
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++;
continue;
}
Expand All @@ -245,7 +253,7 @@ int main(int argc, char **argv)

if (argc == argn) {
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));
return EXIT_FAILURE;
} else {
Expand All @@ -255,14 +263,16 @@ int main(int argc, char **argv)
printf(" *-\n");
}
} else {
for (z = 3; z < argc; z++) {
w = sizeof(hash_buffer);
if ((err = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
return EXIT_FAILURE;
} else {
printf_hex(hash_buffer, w);
printf(" *%s\n", argv[z]);
for (z = argn; z < argc; z++) {
for (y = 0; y < idx; ++y) {
w = sizeof(hash_buffer);
if ((err = hash_file(idxs[y],argv[z],hash_buffer,&w)) != CRYPT_OK) {
fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
return EXIT_FAILURE;
} else {
printf_hex(hash_buffer, w);
printf(" *%s\n", argv[z]);
}
}
}
}
Expand Down

0 comments on commit e1fcd3a

Please sign in to comment.