test: Add 20 files
This commit is contained in:
21
dshash/Makefile
Normal file
21
dshash/Makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
CXX = g++
|
||||
CXXFLAGS = -std=c++17 -O2 -Wall -Wextra -static
|
||||
TARGET = dshash
|
||||
SOURCES = main.cpp ../src/dshash.cpp
|
||||
OBJECTS = main.o dshash.o
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) -o $@ $^
|
||||
|
||||
main.o: main.cpp ../src/dshash.hpp
|
||||
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||
|
||||
dshash.o: ../src/dshash.cpp ../src/dshash.hpp
|
||||
$(CXX) $(CXXFLAGS) -c ../src/dshash.cpp
|
||||
|
||||
clean:
|
||||
rm -f $(OBJECTS) $(TARGET)
|
||||
|
||||
.PHONY: all clean
|
BIN
dshash/dshash
Executable file
BIN
dshash/dshash
Executable file
Binary file not shown.
BIN
dshash/dshash.o
Normal file
BIN
dshash/dshash.o
Normal file
Binary file not shown.
69
dshash/main.cpp
Normal file
69
dshash/main.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "../src/dshash.hpp"
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
void printUsage(const std::string& program) {
|
||||
std::cerr << "Usage: " << program << " [-v] <file_or_directory_path>\n";
|
||||
std::cerr << " -v Verbose mode (list files as they are processed)\n";
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2 || argc > 3) {
|
||||
printUsage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool verbose = false;
|
||||
std::string path;
|
||||
|
||||
if (argc == 3) {
|
||||
if (std::string(argv[1]) != "-v") {
|
||||
printUsage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
verbose = true;
|
||||
path = argv[2];
|
||||
} else {
|
||||
path = argv[1];
|
||||
}
|
||||
|
||||
try {
|
||||
std::filesystem::path target(path);
|
||||
|
||||
if (!std::filesystem::exists(target)) {
|
||||
std::cerr << "Error: Path does not exist: " << path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
DSHash::Hash hash;
|
||||
|
||||
if (std::filesystem::is_regular_file(target)) {
|
||||
if (verbose) {
|
||||
std::cerr << "Processing file: " << target << std::endl;
|
||||
}
|
||||
hash = DSHash::hashFile(target);
|
||||
} else if (std::filesystem::is_directory(target)) {
|
||||
if (verbose) {
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(target)) {
|
||||
if (entry.is_regular_file()) {
|
||||
std::cerr << "Processing: " << entry.path() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
hash = DSHash::hashDirectory(target);
|
||||
} else {
|
||||
std::cerr << "Error: Path is neither a file nor a directory: " << path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << DSHash::toString(hash) << std::endl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
dshash/main.o
Normal file
BIN
dshash/main.o
Normal file
Binary file not shown.
231
src/dshash.cpp
Normal file
231
src/dshash.cpp
Normal file
@@ -0,0 +1,231 @@
|
||||
#include "dshash.hpp"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
const uint32_t DSHash::K[64] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
||||
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
||||
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
||||
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
|
||||
static inline uint32_t rotr(uint32_t x, uint32_t n) {
|
||||
return (x >> n) | (x << (32 - n));
|
||||
}
|
||||
|
||||
static inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) {
|
||||
return (x & y) ^ (~x & z);
|
||||
}
|
||||
|
||||
static inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z) {
|
||||
return (x & y) ^ (x & z) ^ (y & z);
|
||||
}
|
||||
|
||||
static inline uint32_t sigma0(uint32_t x) {
|
||||
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
|
||||
}
|
||||
|
||||
static inline uint32_t sigma1(uint32_t x) {
|
||||
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
|
||||
}
|
||||
|
||||
static inline uint32_t gamma0(uint32_t x) {
|
||||
return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3);
|
||||
}
|
||||
|
||||
static inline uint32_t gamma1(uint32_t x) {
|
||||
return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10);
|
||||
}
|
||||
|
||||
DSHash::DSHash() {
|
||||
h[0] = 0x6a09e667;
|
||||
h[1] = 0xbb67ae85;
|
||||
h[2] = 0x3c6ef372;
|
||||
h[3] = 0xa54ff53a;
|
||||
h[4] = 0x510e527f;
|
||||
h[5] = 0x9b05688c;
|
||||
h[6] = 0x1f83d9ab;
|
||||
h[7] = 0x5be0cd19;
|
||||
}
|
||||
|
||||
void DSHash::update(const uint8_t* data, size_t length) {
|
||||
if (finalized) return;
|
||||
|
||||
total_length += length;
|
||||
|
||||
buffer.insert(buffer.end(), data, data + length);
|
||||
|
||||
while (buffer.size() >= BLOCK_SIZE) {
|
||||
processBlock(buffer.data());
|
||||
buffer.erase(buffer.begin(), buffer.begin() + BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void DSHash::update(const std::string& str) {
|
||||
update(reinterpret_cast<const uint8_t*>(str.data()), str.size());
|
||||
}
|
||||
|
||||
void DSHash::processBlock(const uint8_t* block) {
|
||||
uint32_t w[64];
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
w[i] = (block[i * 4] << 24) |
|
||||
(block[i * 4 + 1] << 16) |
|
||||
(block[i * 4 + 2] << 8) |
|
||||
(block[i * 4 + 3]);
|
||||
}
|
||||
|
||||
for (int i = 16; i < 64; i++) {
|
||||
w[i] = gamma1(w[i - 2]) + w[i - 7] + gamma0(w[i - 15]) + w[i - 16];
|
||||
}
|
||||
|
||||
uint32_t a = h[0];
|
||||
uint32_t b = h[1];
|
||||
uint32_t c = h[2];
|
||||
uint32_t d = h[3];
|
||||
uint32_t e = h[4];
|
||||
uint32_t f = h[5];
|
||||
uint32_t g = h[6];
|
||||
uint32_t hh = h[7];
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
uint32_t t1 = hh + sigma1(e) + ch(e, f, g) + K[i] + w[i];
|
||||
uint32_t t2 = sigma0(a) + maj(a, b, c);
|
||||
hh = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
h[0] += a;
|
||||
h[1] += b;
|
||||
h[2] += c;
|
||||
h[3] += d;
|
||||
h[4] += e;
|
||||
h[5] += f;
|
||||
h[6] += g;
|
||||
h[7] += hh;
|
||||
}
|
||||
|
||||
void DSHash::padMessage() {
|
||||
uint64_t bit_length = total_length * 8;
|
||||
|
||||
buffer.push_back(0x80);
|
||||
|
||||
while ((buffer.size() % 64) != 56) {
|
||||
buffer.push_back(0x00);
|
||||
}
|
||||
|
||||
for (int i = 7; i >= 0; i--) {
|
||||
buffer.push_back((bit_length >> (i * 8)) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::finalize() {
|
||||
if (finalized) {
|
||||
Hash result;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
result[i * 4] = (h[i] >> 24) & 0xff;
|
||||
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
|
||||
result[i * 4 + 2] = (h[i] >> 8) & 0xff;
|
||||
result[i * 4 + 3] = h[i] & 0xff;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
padMessage();
|
||||
|
||||
while (!buffer.empty()) {
|
||||
processBlock(buffer.data());
|
||||
buffer.erase(buffer.begin(), buffer.begin() + BLOCK_SIZE);
|
||||
}
|
||||
|
||||
finalized = true;
|
||||
|
||||
Hash result;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
result[i * 4] = (h[i] >> 24) & 0xff;
|
||||
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
|
||||
result[i * 4 + 2] = (h[i] >> 8) & 0xff;
|
||||
result[i * 4 + 3] = h[i] & 0xff;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::hashString(const std::string& str) {
|
||||
DSHash hasher;
|
||||
hasher.update(str);
|
||||
return hasher.finalize();
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::hashFile(const std::filesystem::path& filepath) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (!file) {
|
||||
throw std::runtime_error("Cannot open file: " + filepath.string());
|
||||
}
|
||||
|
||||
DSHash hasher;
|
||||
constexpr size_t BUFFER_SIZE = 8192;
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
while (file.read(buffer, BUFFER_SIZE) || file.gcount() > 0) {
|
||||
hasher.update(reinterpret_cast<const uint8_t*>(buffer), file.gcount());
|
||||
}
|
||||
|
||||
return hasher.finalize();
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::hashDirectory(const std::filesystem::path& dirpath) {
|
||||
if (!std::filesystem::is_directory(dirpath)) {
|
||||
throw std::runtime_error("Not a directory: " + dirpath.string());
|
||||
}
|
||||
|
||||
DSHash hasher;
|
||||
std::vector<std::filesystem::path> paths;
|
||||
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(dirpath)) {
|
||||
if (entry.is_regular_file()) {
|
||||
paths.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(paths.begin(), paths.end());
|
||||
|
||||
for (const auto& path : paths) {
|
||||
std::string relative = std::filesystem::relative(path, dirpath).string();
|
||||
hasher.update(relative);
|
||||
|
||||
auto fileHash = hashFile(path);
|
||||
hasher.update(fileHash.data(), fileHash.size());
|
||||
}
|
||||
|
||||
return hasher.finalize();
|
||||
}
|
||||
|
||||
std::string DSHash::toString(const Hash& hash) {
|
||||
std::stringstream ss;
|
||||
for (uint8_t byte : hash) {
|
||||
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
41
src/dshash.hpp
Normal file
41
src/dshash.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef DSHASH_HPP
|
||||
#define DSHASH_HPP
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
|
||||
class DSHash {
|
||||
public:
|
||||
using Hash = std::array<uint8_t, 32>;
|
||||
|
||||
DSHash();
|
||||
|
||||
void update(const uint8_t* data, size_t length);
|
||||
void update(const std::string& str);
|
||||
|
||||
Hash finalize();
|
||||
|
||||
static Hash hashString(const std::string& str);
|
||||
static Hash hashFile(const std::filesystem::path& filepath);
|
||||
static Hash hashDirectory(const std::filesystem::path& dirpath);
|
||||
|
||||
static std::string toString(const Hash& hash);
|
||||
|
||||
private:
|
||||
void processBlock(const uint8_t* block);
|
||||
void padMessage();
|
||||
|
||||
static constexpr size_t BLOCK_SIZE = 64;
|
||||
|
||||
std::vector<uint8_t> buffer;
|
||||
uint64_t total_length = 0;
|
||||
uint32_t h[8];
|
||||
bool finalized = false;
|
||||
|
||||
static const uint32_t K[64];
|
||||
};
|
||||
|
||||
#endif
|
100
tests/test.sh
Executable file
100
tests/test.sh
Executable file
@@ -0,0 +1,100 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
DSHASH_BIN="$PROJECT_DIR/dshash/dshash"
|
||||
TEMP_DIR=$(mktemp -d)
|
||||
|
||||
trap "rm -rf $TEMP_DIR" EXIT
|
||||
|
||||
echo "Building dshash utility..."
|
||||
cd "$PROJECT_DIR/dshash"
|
||||
make clean > /dev/null 2>&1
|
||||
make > /dev/null 2>&1
|
||||
|
||||
echo "Building test program..."
|
||||
cd "$SCRIPT_DIR"
|
||||
g++ -std=c++17 -o test_lib test_lib.cpp ../src/dshash.cpp -I../src
|
||||
|
||||
FAILED=0
|
||||
PASSED=0
|
||||
|
||||
run_test() {
|
||||
local test_name="$1"
|
||||
local expected="$2"
|
||||
local actual="$3"
|
||||
|
||||
if [ "$expected" = "$actual" ]; then
|
||||
echo "✓ $test_name"
|
||||
PASSED=$((PASSED + 1))
|
||||
else
|
||||
echo "✗ $test_name"
|
||||
echo " Expected: $expected"
|
||||
echo " Got: $actual"
|
||||
FAILED=$((FAILED + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
echo ""
|
||||
echo "Running library tests..."
|
||||
./test_lib
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "✓ All library tests passed"
|
||||
PASSED=$((PASSED + 1))
|
||||
else
|
||||
echo "✗ Library tests failed"
|
||||
FAILED=$((FAILED + 1))
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Running utility tests..."
|
||||
|
||||
echo -n "abc" > "$TEMP_DIR/test1.txt"
|
||||
HASH=$($DSHASH_BIN "$TEMP_DIR/test1.txt")
|
||||
run_test "Hash of 'abc'" "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "$HASH"
|
||||
|
||||
echo -n "" > "$TEMP_DIR/empty.txt"
|
||||
HASH=$($DSHASH_BIN "$TEMP_DIR/empty.txt")
|
||||
run_test "Hash of empty file" "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" "$HASH"
|
||||
|
||||
echo -n "The quick brown fox jumps over the lazy dog" > "$TEMP_DIR/fox.txt"
|
||||
HASH=$($DSHASH_BIN "$TEMP_DIR/fox.txt")
|
||||
run_test "Hash of 'The quick brown fox...'" "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592" "$HASH"
|
||||
|
||||
mkdir -p "$TEMP_DIR/testdir/subdir"
|
||||
echo -n "file1" > "$TEMP_DIR/testdir/file1.txt"
|
||||
echo -n "file2" > "$TEMP_DIR/testdir/subdir/file2.txt"
|
||||
HASH=$($DSHASH_BIN "$TEMP_DIR/testdir")
|
||||
run_test "Hash of directory" "$(echo -n "$HASH" | grep -E '^[a-f0-9]{64}$' > /dev/null && echo 'valid')" "valid"
|
||||
|
||||
echo -n "test" > "$TEMP_DIR/verbose_test.txt"
|
||||
OUTPUT=$($DSHASH_BIN -v "$TEMP_DIR/verbose_test.txt" 2>&1)
|
||||
if echo "$OUTPUT" | grep -q "Processing file:"; then
|
||||
run_test "Verbose mode" "works" "works"
|
||||
else
|
||||
run_test "Verbose mode" "works" "failed"
|
||||
fi
|
||||
|
||||
mkdir -p "$TEMP_DIR/verbose_dir"
|
||||
echo -n "test" > "$TEMP_DIR/verbose_dir/file.txt"
|
||||
OUTPUT=$($DSHASH_BIN -v "$TEMP_DIR/verbose_dir" 2>&1)
|
||||
if echo "$OUTPUT" | grep -q "Processing:"; then
|
||||
run_test "Verbose mode for directory" "works" "works"
|
||||
else
|
||||
run_test "Verbose mode for directory" "works" "failed"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "========================================="
|
||||
echo "Test Results: $PASSED passed, $FAILED failed"
|
||||
echo "========================================="
|
||||
|
||||
if [ $FAILED -eq 0 ]; then
|
||||
echo "All tests passed!"
|
||||
exit 0
|
||||
else
|
||||
echo "Some tests failed!"
|
||||
exit 1
|
||||
fi
|
BIN
tests/test_lib
Executable file
BIN
tests/test_lib
Executable file
Binary file not shown.
125
tests/test_lib.cpp
Normal file
125
tests/test_lib.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "../src/dshash.hpp"
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <cstring>
|
||||
|
||||
void test_string_hash() {
|
||||
auto hash = DSHash::hashString("abc");
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
std::cout << "✓ String hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_empty_string() {
|
||||
auto hash = DSHash::hashString("");
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
assert(hashStr == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||
std::cout << "✓ Empty string hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_long_string() {
|
||||
auto hash = DSHash::hashString("The quick brown fox jumps over the lazy dog");
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
assert(hashStr == "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
|
||||
std::cout << "✓ Long string hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_incremental_update() {
|
||||
DSHash hasher;
|
||||
hasher.update("a");
|
||||
hasher.update("b");
|
||||
hasher.update("c");
|
||||
auto hash = hasher.finalize();
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
assert(hashStr == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
std::cout << "✓ Incremental update test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_file_hash() {
|
||||
std::string tempFile = "/tmp/test_hash_file.txt";
|
||||
std::ofstream out(tempFile);
|
||||
out << "Test content for hashing";
|
||||
out.close();
|
||||
|
||||
auto hash = DSHash::hashFile(tempFile);
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
|
||||
DSHash hasher;
|
||||
hasher.update("Test content for hashing");
|
||||
auto expectedHash = hasher.finalize();
|
||||
std::string expectedHashStr = DSHash::toString(expectedHash);
|
||||
|
||||
assert(hashStr == expectedHashStr);
|
||||
std::filesystem::remove(tempFile);
|
||||
std::cout << "✓ File hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_large_data() {
|
||||
DSHash hasher;
|
||||
std::string chunk(1000, 'a');
|
||||
for (int i = 0; i < 100; i++) {
|
||||
hasher.update(chunk);
|
||||
}
|
||||
auto hash = hasher.finalize();
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
|
||||
assert(hashStr.length() == 64);
|
||||
for (char c : hashStr) {
|
||||
assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
|
||||
}
|
||||
std::cout << "✓ Large data hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_known_vectors() {
|
||||
struct TestVector {
|
||||
std::string input;
|
||||
std::string expected;
|
||||
};
|
||||
|
||||
TestVector vectors[] = {
|
||||
{"a", "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"},
|
||||
{"message digest", "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650"},
|
||||
{"abcdefghijklmnopqrstuvwxyz", "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"},
|
||||
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
||||
"db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0"}
|
||||
};
|
||||
|
||||
for (const auto& vec : vectors) {
|
||||
auto hash = DSHash::hashString(vec.input);
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
assert(hashStr == vec.expected);
|
||||
}
|
||||
std::cout << "✓ Known test vectors passed" << std::endl;
|
||||
}
|
||||
|
||||
void test_binary_data() {
|
||||
uint8_t binaryData[] = {0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD};
|
||||
DSHash hasher;
|
||||
hasher.update(binaryData, sizeof(binaryData));
|
||||
auto hash = hasher.finalize();
|
||||
std::string hashStr = DSHash::toString(hash);
|
||||
|
||||
assert(hashStr.length() == 64);
|
||||
std::cout << "✓ Binary data hash test passed" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
test_string_hash();
|
||||
test_empty_string();
|
||||
test_long_string();
|
||||
test_incremental_update();
|
||||
test_file_hash();
|
||||
test_large_data();
|
||||
test_known_vectors();
|
||||
test_binary_data();
|
||||
|
||||
std::cout << "\nAll library tests passed!" << std::endl;
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Test failed with exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user