SPDX-FileCopyrightText: 2021 Bastian Bittorf SPDX-FileCopyrightText: 2021 fosslinux SPDX-License-Identifier: Unlicense This adds a main() function to this tool, giving a frontend so we can call it from the command line. --- sha-256.c 2019-08-04 22:20:53.000000000 +1000 +++ sha-256.c 2021-02-15 17:21:27.478945543 +1100 @@ -1,10 +1,16 @@ #include #include - -#include "sha-256.h" +#include #define CHUNK_SIZE 64 #define TOTAL_LEN_LEN 8 +#define MAX_FILE_SIZE 2*1024*1024 + +void show_usage(void) +{ + printf("Usage: sha256sum \n"); + printf(" sha256sum -c \n"); +} /* * ABOUT bool: this file does not use bool in order to be as pre-C99 compatible as possible. @@ -217,3 +223,121 @@ hash[j++] = (uint8_t) h[i]; } } + +static void hash_to_string(char hash_string[66], uint8_t hash[32]) +{ + for (size_t i = 0; i < 32; i++) { + hash_string += sprintf(hash_string, "%02x", hash[i]); + } + hash_string[65] = '\0'; +} + +static void hash_as_string(char filename[128], char hash_string[66]) +{ + char buffer[MAX_FILE_SIZE]; + + FILE* fp = fopen(filename, "r"); + if (fp == NULL) + { + printf("Opening file %s failed!", filename); + hash_string = NULL; + return; + } + + size_t i; + for (i = 0; i < MAX_FILE_SIZE; i++) { + int c = fgetc(fp); + if (c == EOF) { + break; + } + buffer[i] = c; + } + + uint8_t hash[32]; + + calc_sha_256(hash, buffer, i); + hash_to_string(hash_string, hash); +} + +int main(int argc, char **argv) +{ + char buffer[MAX_FILE_SIZE]; + size_t i; + FILE *fp; + FILE *csum_fp; + + if (argc == 1) { + show_usage(); + return 2; + } + + if ((strcmp(argv[1], "-c") == 0) || (strcmp(argv[1], "--check") == 0)) { + /* Operate in check mode */ + int failed = 0; + + /* Get and open checksum file */ + char *csum_filename = argv[2]; + csum_fp = fopen(csum_filename, "r"); + if (csum_fp == NULL) { + printf("Opening file %s failed!", csum_filename); + }; + + /* We break out of this loop at EOF */ + while (1) { + /* Get the hash we need to check against */ + char hash_true[256]; + char filename[512]; + int c = fgetc(csum_fp); + i = 0; + while (c != ' ' && c != EOF) { + hash_true[i] = c; + c = fgetc(csum_fp); + i++; + } + if(c == EOF) break; + /* Skip the next space(s) */ + while (c == ' ') { + c = fgetc(csum_fp); + } + if(c == EOF) break; + /* Get the filename */ + i = 0; + while (c != '\n' && c != EOF) { + filename[i] = c; + c = fgetc(csum_fp); + i++; + } + filename[i] = '\0'; + + /* Break out @ EOF */ + if (c == EOF) break; + + /* Get the hash */ + char hash_string[66]; + hash_as_string(filename, hash_string); + if (hash_string == NULL) return 127; + + /* Output results */ + printf("%s: ", filename); + if (strcmp(hash_true, hash_string)) { + printf("FAILED\n"); + failed = 1; + } else { + printf("OK\n"); + } + } + + if (failed) return 1; + } else { + /* All the arguments are files to generate checksums for */ + + for (i = 1; i < argc; i++) { + char hash[66]; + hash_as_string(argv[i], hash); + if (hash == NULL) return 127; + printf("%s %s\n", hash, argv[i]); + } + } + + return 0; +}