1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 21:41:48 +02:00

* Add SHA-256.

* Tests for the various hashes.
This commit is contained in:
Eelco Dolstra 2005-01-14 12:03:04 +00:00
parent 37b51a9aa6
commit 63791eb05b
9 changed files with 912 additions and 8 deletions

View file

@ -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);
}