about summary refs log tree commit diff
path: root/src/hash.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-06-16T13·33+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-06-16T13·33+0000
commit822794001cb4260b8c04a7bd2d50d890edae709a (patch)
treec4c3a86f638422c8d756752050ebcbb45eba2ee7 /src/hash.cc
parentb9f09b3268bf0c3d9ecd512dd3a0aa1247550cc2 (diff)
* Started implementing the new evaluation model.
* Lots of refactorings.
* Unit tests.

Diffstat (limited to 'src/hash.cc')
-rw-r--r--src/hash.cc15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/hash.cc b/src/hash.cc
index 25d76bd15c..bb25c5168f 100644
--- a/src/hash.cc
+++ b/src/hash.cc
@@ -46,6 +46,8 @@ Hash::operator string() const
 Hash parseHash(const string & s)
 {
     Hash hash;
+    if (s.length() != Hash::hashSize * 2)
+        throw BadRefError("invalid hash: " + s);
     for (unsigned int i = 0; i < Hash::hashSize; i++) {
         string s2(s, i * 2, 2);
         if (!isxdigit(s2[0]) || !isxdigit(s2[1])) 
@@ -74,14 +76,23 @@ bool isHash(const string & s)
 
 
 /* Compute the MD5 hash of a file. */
+Hash hashString(const string & s)
+{
+    Hash hash;
+    md5_buffer(s.c_str(), s.length(), hash.hash);
+    return hash;
+}
+
+
+/* Compute the MD5 hash of a file. */
 Hash hashFile(const string & fileName)
 {
     Hash hash;
     FILE * file = fopen(fileName.c_str(), "rb");
     if (!file)
-        throw Error("file `" + fileName + "' does not exist");
+        throw SysError("file `" + fileName + "' does not exist");
     int err = md5_stream(file, hash.hash);
     fclose(file);
-    if (err) throw Error("cannot hash file");
+    if (err) throw SysError("cannot hash file " + fileName);
     return hash;
 }