test: Update 8 files
This commit is contained in:
189
src/dshash.cpp
189
src/dshash.cpp
@@ -52,34 +52,6 @@ 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];
|
||||
|
||||
@@ -126,6 +98,23 @@ void DSHash::processBlock(const uint8_t* block) {
|
||||
h[7] += hh;
|
||||
}
|
||||
|
||||
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::padMessage() {
|
||||
uint64_t bit_length = total_length * 8;
|
||||
|
||||
@@ -140,9 +129,9 @@ void DSHash::padMessage() {
|
||||
}
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::finalize() {
|
||||
tHash DSHash::finalize() {
|
||||
if (finalized) {
|
||||
Hash result;
|
||||
tHash result;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
result[i * 4] = (h[i] >> 24) & 0xff;
|
||||
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
|
||||
@@ -161,7 +150,7 @@ DSHash::Hash DSHash::finalize() {
|
||||
|
||||
finalized = true;
|
||||
|
||||
Hash result;
|
||||
tHash result;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
result[i * 4] = (h[i] >> 24) & 0xff;
|
||||
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
|
||||
@@ -172,35 +161,80 @@ DSHash::Hash DSHash::finalize() {
|
||||
return result;
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::hashString(const std::string& str) {
|
||||
DSHash hasher;
|
||||
hasher.update(str);
|
||||
return hasher.finalize();
|
||||
std::string DSHash::hashString(const std::string& str) {
|
||||
h[0] = 0x6a09e667;
|
||||
h[1] = 0xbb67ae85;
|
||||
h[2] = 0x3c6ef372;
|
||||
h[3] = 0xa54ff53a;
|
||||
h[4] = 0x510e527f;
|
||||
h[5] = 0x9b05688c;
|
||||
h[6] = 0x1f83d9ab;
|
||||
h[7] = 0x5be0cd19;
|
||||
buffer.clear();
|
||||
total_length = 0;
|
||||
finalized = false;
|
||||
|
||||
update(str);
|
||||
tHash hash = finalize();
|
||||
|
||||
std::stringstream ss;
|
||||
for (uint8_t byte : hash) {
|
||||
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::hashFile(const std::filesystem::path& filepath) {
|
||||
std::string 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;
|
||||
h[0] = 0x6a09e667;
|
||||
h[1] = 0xbb67ae85;
|
||||
h[2] = 0x3c6ef372;
|
||||
h[3] = 0xa54ff53a;
|
||||
h[4] = 0x510e527f;
|
||||
h[5] = 0x9b05688c;
|
||||
h[6] = 0x1f83d9ab;
|
||||
h[7] = 0x5be0cd19;
|
||||
buffer.clear();
|
||||
total_length = 0;
|
||||
finalized = false;
|
||||
|
||||
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());
|
||||
update(reinterpret_cast<const uint8_t*>(buffer), file.gcount());
|
||||
}
|
||||
|
||||
return hasher.finalize();
|
||||
tHash hash = finalize();
|
||||
|
||||
std::stringstream ss;
|
||||
for (uint8_t byte : hash) {
|
||||
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
DSHash::Hash DSHash::hashDirectory(const std::filesystem::path& dirpath) {
|
||||
std::string DSHash::hashDirectory(const std::filesystem::path& dirpath) {
|
||||
if (!std::filesystem::is_directory(dirpath)) {
|
||||
throw std::runtime_error("Not a directory: " + dirpath.string());
|
||||
}
|
||||
|
||||
DSHash hasher;
|
||||
h[0] = 0x6a09e667;
|
||||
h[1] = 0xbb67ae85;
|
||||
h[2] = 0x3c6ef372;
|
||||
h[3] = 0xa54ff53a;
|
||||
h[4] = 0x510e527f;
|
||||
h[5] = 0x9b05688c;
|
||||
h[6] = 0x1f83d9ab;
|
||||
h[7] = 0x5be0cd19;
|
||||
buffer.clear();
|
||||
total_length = 0;
|
||||
finalized = false;
|
||||
|
||||
std::vector<std::filesystem::path> paths;
|
||||
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(dirpath)) {
|
||||
@@ -213,19 +247,78 @@ DSHash::Hash DSHash::hashDirectory(const std::filesystem::path& dirpath) {
|
||||
|
||||
for (const auto& path : paths) {
|
||||
std::string relative = std::filesystem::relative(path, dirpath).string();
|
||||
hasher.update(relative);
|
||||
update(relative);
|
||||
|
||||
auto fileHash = hashFile(path);
|
||||
hasher.update(fileHash.data(), fileHash.size());
|
||||
DSHash fileHasher(path);
|
||||
std::string fileHashStr = fileHasher.toString();
|
||||
update(fileHashStr);
|
||||
}
|
||||
|
||||
return hasher.finalize();
|
||||
}
|
||||
|
||||
std::string DSHash::toString(const Hash& hash) {
|
||||
tHash hash = finalize();
|
||||
|
||||
std::stringstream ss;
|
||||
for (uint8_t byte : hash) {
|
||||
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
DSHash::DSHash(const std::filesystem::path& path) {
|
||||
h[0] = 0x6a09e667;
|
||||
h[1] = 0xbb67ae85;
|
||||
h[2] = 0x3c6ef372;
|
||||
h[3] = 0xa54ff53a;
|
||||
h[4] = 0x510e527f;
|
||||
h[5] = 0x9b05688c;
|
||||
h[6] = 0x1f83d9ab;
|
||||
h[7] = 0x5be0cd19;
|
||||
|
||||
if (std::filesystem::is_regular_file(path)) {
|
||||
hashFile(path);
|
||||
} else if (std::filesystem::is_directory(path)) {
|
||||
hashDirectory(path);
|
||||
} else {
|
||||
throw std::runtime_error("Path is neither file nor directory: " + path.string());
|
||||
}
|
||||
}
|
||||
|
||||
DSHash::DSHash(const std::string& str) {
|
||||
h[0] = 0x6a09e667;
|
||||
h[1] = 0xbb67ae85;
|
||||
h[2] = 0x3c6ef372;
|
||||
h[3] = 0xa54ff53a;
|
||||
h[4] = 0x510e527f;
|
||||
h[5] = 0x9b05688c;
|
||||
h[6] = 0x1f83d9ab;
|
||||
h[7] = 0x5be0cd19;
|
||||
|
||||
hashString(str);
|
||||
}
|
||||
|
||||
std::string DSHash::toString() {
|
||||
if (!finalized) {
|
||||
finalize();
|
||||
}
|
||||
|
||||
tHash hash = get();
|
||||
std::stringstream ss;
|
||||
for (uint8_t byte : hash) {
|
||||
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
tHash DSHash::get() {
|
||||
if (!finalized) {
|
||||
return finalize();
|
||||
}
|
||||
|
||||
tHash 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;
|
||||
}
|
@@ -7,23 +7,25 @@
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
|
||||
typedef std::array<uint8_t, 32> tHash;
|
||||
|
||||
class DSHash {
|
||||
public:
|
||||
using Hash = std::array<uint8_t, 32>;
|
||||
public:
|
||||
explicit DSHash(const std::filesystem::path& path);
|
||||
explicit DSHash(const std::string& str);
|
||||
|
||||
DSHash();
|
||||
std::string toString();
|
||||
tHash get();
|
||||
|
||||
private:
|
||||
std::string hashString(const std::string& str);
|
||||
std::string hashFile(const std::filesystem::path& filepath);
|
||||
std::string hashDirectory(const std::filesystem::path& dirpath);
|
||||
|
||||
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);
|
||||
|
||||
tHash finalize();
|
||||
|
||||
private:
|
||||
void processBlock(const uint8_t* block);
|
||||
void padMessage();
|
||||
|
Reference in New Issue
Block a user