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-14 12:03:04 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2005-01-14 12:03:04 +0000
commit63791eb05b236afcea510d769a8943f8be3dbc9c (patch)
tree48b25ce1d2336bc41ab838475d915a7d01134e98 /src/libutil/hash.cc
parent37b51a9aa6d5d0fb97bad0d1a27d27304da277c2 (diff)
downloadguix-63791eb05b236afcea510d769a8943f8be3dbc9c.tar.gz
* Add SHA-256.
* Tests for the various hashes.

Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 46334ad820..599d375dca 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -3,6 +3,7 @@
 extern "C" {
 #include "md5.h"
 #include "sha1.h"
+#include "sha256.h"
 }
 
 #include "hash.hh"
@@ -19,6 +20,7 @@ Hash::Hash(HashType type)
     this->type = type;
     if (type == htMD5) hashSize = md5HashSize;
     else if (type == htSHA1) hashSize = sha1HashSize;
+    else if (type == htSHA256) hashSize = sha256HashSize;
     else throw Error("unknown hash type");
     memset(hash, 0, hashSize);
 }
@@ -96,6 +98,7 @@ struct Ctx
 {
     md5_ctx md5;
     sha_ctx sha1;
+    SHA256_CTX sha256;
 };
 
 
@@ -103,6 +106,7 @@ static void start(HashType ht, Ctx & ctx)
 {
     if (ht == htMD5) md5_init_ctx(&ctx.md5);
     else if (ht == htSHA1) sha_init(&ctx.sha1);
+    else if (ht == htSHA256) SHA256_Init(&ctx.sha256);
 }
 
 
@@ -111,6 +115,7 @@ static void update(HashType ht, Ctx & ctx,
 {
     if (ht == htMD5) md5_process_bytes(bytes, len, &ctx.md5);
     else if (ht == htSHA1) sha_update(&ctx.sha1, bytes, len);
+    else if (ht == htSHA256) SHA256_Update(&ctx.sha256, bytes, len);
 }
 
 
@@ -121,6 +126,7 @@ static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
         sha_final(&ctx.sha1);
         sha_digest(&ctx.sha1, hash);
     }
+    else if (ht == htSHA256) SHA256_Final(hash, &ctx.sha256);
 }