2022-06-29 19:03:12 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2022-06-29 21:29:15 +02:00
|
|
|
#include "../lib/helper.h"
|
|
|
|
#include "../lib/io.h"
|
|
|
|
#include "../lib/md2.h"
|
2022-06-29 10:50:55 +02:00
|
|
|
|
2022-06-29 19:03:12 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:50:55 +02:00
|
|
|
int main(int argc, char** argv) {
|
2022-06-29 11:12:35 +02:00
|
|
|
struct configuration c;
|
|
|
|
enum argumentParseResult result = parseArguments(argc, argv, &c);
|
2022-06-29 10:50:55 +02:00
|
|
|
|
2022-06-29 11:12:35 +02:00
|
|
|
switch (result) {
|
2022-06-29 10:50:55 +02:00
|
|
|
case RESULT_EXIT_SUCCESS:
|
2022-06-29 11:12:35 +02:00
|
|
|
return EXIT_SUCCESS;
|
2022-06-29 10:50:55 +02:00
|
|
|
case RESULT_EXIT_FAILURE:
|
2022-06-29 11:12:35 +02:00
|
|
|
return EXIT_FAILURE;
|
2022-06-29 10:50:55 +02:00
|
|
|
default:
|
2022-06-29 11:12:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-06-29 10:50:55 +02:00
|
|
|
|
2022-06-29 11:12:35 +02:00
|
|
|
printf(
|
|
|
|
"Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, "
|
|
|
|
"benchmark cycles: %d\n",
|
|
|
|
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
|
2022-06-29 10:50:55 +02:00
|
|
|
|
2022-06-29 20:54:41 +02:00
|
|
|
// 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");
|
2022-06-29 19:03:12 +02:00
|
|
|
|
2022-06-29 15:41:56 +02:00
|
|
|
uint8_t out[16];
|
2022-06-29 20:54:41 +02:00
|
|
|
md2_hash(len, data, out);
|
2022-06-29 16:12:09 +02:00
|
|
|
|
|
|
|
printf("Hash: ");
|
2022-06-29 21:45:45 +02:00
|
|
|
char hash[32];
|
2022-06-29 20:54:41 +02:00
|
|
|
encodeHash(out, hash);
|
|
|
|
printf("%s\n", hash);
|
2022-06-29 15:41:56 +02:00
|
|
|
|
2022-06-29 21:45:45 +02:00
|
|
|
free(data);
|
|
|
|
|
2022-06-29 11:12:35 +02:00
|
|
|
return 0;
|
2022-06-29 10:50:55 +02:00
|
|
|
}
|