From 0c4fc1e72770fc78d87891e5c031272fca59e409 Mon Sep 17 00:00:00 2001 From: zyp Date: Tue, 9 May 2006 20:33:27 +0000 Subject: [project @ zyp-20060509203327-c8c93b489da2ea46] [project @ 27] Multihash library rewrite complete. --- src/hash.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/hash.cpp (limited to 'src/hash.cpp') diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..89b14cc --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1,63 @@ +#include "hash.h" +#include "crc32.h" + +#include +#include + +namespace Multihash { + namespace Hex { + static char* digits = "0123456789abcdef"; + std::string hex(char* bin, int length) { + std::string s(length * 2, ' '); + for(int i = 0; i < length; i++) { + s[i*2] = digits[(bin[i] >> 4) & 0xf]; + s[i*2+1] = digits[bin[i] & 0xf]; + } + return s; + } + std::string hex(int bin) { + std::string s(sizeof(int) * 2, ' '); + for(int i = 0; i < sizeof(int) * 2; i++) { + s[sizeof(int) * 2 - 1 - i] = digits[bin & 0xf]; + bin = bin >> 4; + } + return s; + } + } + + Hash::Hash(Hash* n) { + digest_str = ""; + next = n; + } + + void Hash::update(std::string data) { + const char* buf = data.c_str(); + int len = data.length(); + this->_update(buf, len); + } + + void Hash::_update(const char* data, int length) { + if(digest_str.length()) { + throw std::runtime_error("Can't update after digest."); + } + this->hash_update(data, length); + if(next) { + next->_update(data, length); + } + } + + std::string Hash::digest() { + if(!digest_str.length()) { + digest_str = this->hash_digest(); + } + return digest_str; + } + + void Hash::hash_update(const char* data, int length) { + throw std::runtime_error("Not implemented."); + } + + std::string Hash::hash_digest() { + throw std::runtime_error("Not implemented."); + } +} -- cgit v1.2.3