about summary refs log tree commit diff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2005-01-13T15·44+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2005-01-13T15·44+0000
commit73992371a3bc16b27b22e53d5f7ae600dea9cf60 (patch)
treed1e5db8cea5caacff34ac4e9b61195b97dbd9ceb /src/libutil/hash.cc
parentd46b4262dc84689c3916583b91ed9fc6dafefdd6 (diff)
* Refactoring to support SHA-1.
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 752b269125..1a44c85341 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -8,14 +8,18 @@ extern "C" {
 #include "archive.hh"
 
 
-Hash::Hash()
+Hash::Hash(HashType type)
 {
-    memset(hash, 0, sizeof(hash));
+    this->type = type;
+    if (type == htMD5) hashSize = md5HashSize;
+    else if (type == htSHA1) hashSize = sha1HashSize;
+    memset(hash, 0, hashSize);
 }
 
 
 bool Hash::operator == (const Hash & h2) const
 {
+    if (hashSize != h2.hashSize) return false;
     for (unsigned int i = 0; i < hashSize; i++)
         if (hash[i] != h2.hash[i]) return false;
     return true;
@@ -52,10 +56,10 @@ Hash::operator string() const
     
 Hash parseHash(const string & s)
 {
-    Hash hash;
-    if (s.length() != Hash::hashSize * 2)
+    Hash hash(htMD5);
+    if (s.length() != hash.hashSize * 2)
         throw Error(format("invalid hash `%1%'") % s);
-    for (unsigned int i = 0; i < Hash::hashSize; i++) {
+    for (unsigned int i = 0; i < hash.hashSize; i++) {
         string s2(s, i * 2, 2);
         if (!isxdigit(s2[0]) || !isxdigit(s2[1])) 
             throw Error(format("invalid hash `%1%'") % s);
@@ -83,7 +87,7 @@ bool isHash(const string & s)
 
 Hash hashString(const string & s)
 {
-    Hash hash;
+    Hash hash(htMD5);
     md5_buffer(s.c_str(), s.length(), hash.hash);
     return hash;
 }
@@ -91,7 +95,7 @@ Hash hashString(const string & s)
 
 Hash hashFile(const Path & path)
 {
-    Hash hash;
+    Hash hash(htMD5);
     FILE * file = fopen(path.c_str(), "rb");
     if (!file)
         throw SysError(format("file `%1%' does not exist") % path);
@@ -115,7 +119,7 @@ struct HashSink : DumpSink
 
 Hash hashPath(const Path & path)
 {
-    Hash hash;
+    Hash hash(htMD5);
     HashSink sink;
     md5_init_ctx(&sink.ctx);
     dumpPath(path, sink);