summaryrefslogtreecommitdiff
path: root/src/hash.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/hash.cpp')
-rw-r--r--src/hash.cpp63
1 files changed, 63 insertions, 0 deletions
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 <stdexcept>
+#include <iostream>
+
+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.");
+ }
+}