gra-projekt/Implementierung/src/main.c

88 lines
2.5 KiB
C

#include <stdbool.h>
#include "../lib/helper.h"
#include "../lib/io.h"
#include "../lib/md2.h"
// Returns true when val is approx. equal to exp.
static bool runTest(const char* message, const char* expectedHash) {
uint8_t out[16];
md2_hash(strlen(message), message, out);
char hash[32];
encodeHash(out, hash);
bool ok = !strcmp(hash, expectedHash);
printf("%s: md2(%s) %s == %s\n", "not ok" + (4 * ok), message, hash,
expectedHash);
return ok;
}
unsigned runTests(void) {
unsigned failed = 0;
// Note: cases with n<2 are not tested here.
failed += !runTest("", "8350e5a3e24c153df2275c9f80692773");
failed += !runTest("a", "32ec01ec4a6dac72c0ab96fb34c0b5d1");
failed += !runTest("abc", "da853b0d3f88d99b30283a69e6ded6bb");
failed += !runTest("message digest", "ab4f496bfb2a530b219ff33031fe06b0");
failed += !runTest("jebdjcslfhwfdig", "e1b69085c6f6e36cb8fe8d98ed3f2c35");
failed += !runTest("0123456789abcde", "d95629645108a20ab4d70e8545e0723b");
failed += !runTest("0123456789abcdef", "12c8dfa285f14e1af8c5254e7092d0d3");
failed += !runTest("0123456789abcdefg", "e4d0efded5ef7b6843a5ba47e1171347");
failed += !runTest("abcdefghijklmnopqrstuvwxyz",
"4e8ddff3650292ab5a4108c3aa47940b");
failed +=
!runTest("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"da33def2a42df13975352846c30338cd");
failed += !runTest(
"123456789012345678901234567890123456789012345678901234567890123456789012"
"34567890",
"d5976f79d83d3a0dc9806c3c66f3efd8");
if (failed)
printf("%u tests FAILED\n", failed);
else
printf("All tests PASSED\n");
return failed;
}
int main(int argc, char** argv) {
struct configuration c;
enum argumentParseResult result = parseArguments(argc, argv, &c);
switch (result) {
case RESULT_EXIT_SUCCESS:
return EXIT_SUCCESS;
case RESULT_EXIT_FAILURE:
return EXIT_FAILURE;
default:
break;
}
printf(
"Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, "
"benchmark cycles: %d\n",
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
// runTests();
size_t len;
uint8_t* data;
if (!read_file(c.filename, &data, &len)) {
printf("Error reading file %s!", c.filename);
return EXIT_FAILURE;
}
printf("File read with size: %zu\n", len);
printf("\n");
uint8_t out[16];
md2_hash(len, data, out);
printf("Hash: ");
char hash[16];
encodeHash(out, hash);
printf("%s\n", hash);
return 0;
}