Skip to content

Commit 11987e5

Browse files
author
Martin D. Weinberg
committed
Added missing md5 hash test code
1 parent cc7f53e commit 11987e5

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

utils/Test/testmd5.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This test verifies that QuickDigest5 correctly computes the MD5
2+
// hash of a file
3+
4+
#include <iostream>
5+
#include <filesystem>
6+
7+
#include "quickdigest5.hpp"
8+
9+
#include "exputils.H" // For get_md5sum which uses the system's md5sum
10+
// command for verification
11+
12+
int main(int argc, char* argv[])
13+
{
14+
// Default to example.txt if no argument
15+
std::string filePath = argc > 1 ? argv[1] : "example.txt";
16+
17+
// Check if the file exists before trying to hash it
18+
std::filesystem::path p(filePath);
19+
if (!std::filesystem::exists(p)) {
20+
std::cout << "File <" << filePath << "> not found." << std::endl
21+
<< "Usage: " << argv[0] << " [file_path]" << std::endl
22+
<< "Defaulting to example.txt if it exists." << std::endl;
23+
}
24+
25+
// One-line method to get the hex digest of a file
26+
std::string hash = QuickDigest5::fileToHash(filePath);
27+
28+
if (!hash.empty()) {
29+
std::cout << "MD5: " << hash << std::endl;
30+
} else {
31+
std::cerr << "Error: Could not process file." << std::endl;
32+
}
33+
34+
// System version of md5sum for comparison
35+
try {
36+
std::string systemHash = get_md5sum(filePath);
37+
std::cout << "System md5sum: " << systemHash << std::endl;
38+
if (hash == systemHash) {
39+
std::cout << "Success: hashes match!" << std::endl;
40+
} else {
41+
std::cerr << "Error: hashes do not match!" << std::endl;
42+
}
43+
} catch (const std::exception& e) {
44+
std::cerr << "Error computing system md5sum: " << e.what() << std::endl;
45+
}
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)