-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (48 loc) · 1.82 KB
/
main.cpp
File metadata and controls
56 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Hashing demo: use hashing::md5 and hashing::sha256 from separate translation units
#include <iostream>
#include <string>
#include <vector>
#include "hashing/md5.hpp"
#include "hashing/sha256.hpp"
int main() {
try {
using hashing::md5;
using hashing::sha256;
struct Test {
std::string input;
std::string expect_md5;
std::string expect_sha256;
};
std::vector<Test> tests = {
{"",
"d41d8cd98f00b204e9800998ecf8427e",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{"abc",
"900150983cd24fb0d6963f7d28e17f72",
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"},
{"The quick brown fox jumps over the lazy dog",
"9e107d9d372bb6826bd81d3542a419d6",
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"}
};
bool all_ok = true;
for (const auto& t : tests) {
auto md5_hex = md5(t.input);
auto sha_hex = sha256(t.input);
bool ok_md5 = (md5_hex == t.expect_md5);
bool ok_sha = (sha_hex == t.expect_sha256);
all_ok = all_ok && ok_md5 && ok_sha;
std::cout << "Input: '" << t.input << "'\n";
std::cout << " MD5 = " << md5_hex << (ok_md5 ? " (OK)" : " (MISMATCH)") << "\n";
std::cout << " SHA256 = " << sha_hex << (ok_sha ? " (OK)" : " (MISMATCH)") << "\n\n";
}
if (!all_ok) {
std::cerr << "One or more test vectors failed." << std::endl;
return 1;
}
std::cout << "All hash tests passed." << std::endl;
return 0;
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
return 2;
}
}