Migrate free SHA* functions to SHA*Val::digest().

This commit is contained in:
Matthias Vallentin 2012-12-12 10:28:56 -08:00
parent 91f2cb2b64
commit ddd306f00f
5 changed files with 80 additions and 81 deletions

@ -1 +1 @@
Subproject commit 06682dbb15d26d2688bdc9ad76efec17d38dc80f
Subproject commit a8846fc5b004ffe4e3d00e826d0077ba19518192

@ -1 +1 @@
Subproject commit 91e3b8ad445cb4d8919b4ee1cc6f0753c3fa0a55
Subproject commit 834131cd0ec0f63cce9de818726fe6167dedbf34

View file

@ -128,6 +128,28 @@ bool MD5Val::DoUnserialize(UnserialInfo* info)
}
void SHA1Val::digest(val_list& vlist, u_char result[SHA_DIGEST_LENGTH])
{
SHA_CTX h;
sha1_init(&h);
loop_over_list(vlist, i)
{
Val* v = vlist[i];
if ( v->Type()->Tag() == TYPE_STRING )
{
const BroString* str = v->AsString();
sha1_update(&h, str->Bytes(), str->Len());
}
else
{
ODesc d(DESC_BINARY);
v->Describe(&d);
sha1_update(&h, (const u_char *) d.Bytes(), d.Len());
}
}
sha1_final(&h, result);
}
bool SHA1Val::Init()
{
sha1_init(&ctx);
@ -164,6 +186,28 @@ bool SHA1Val::DoUnserialize(UnserialInfo* info)
}
void SHA256Val::digest(val_list& vlist, u_char result[SHA256_DIGEST_LENGTH])
{
SHA256_CTX h;
sha256_init(&h);
loop_over_list(vlist, i)
{
Val* v = vlist[i];
if ( v->Type()->Tag() == TYPE_STRING )
{
const BroString* str = v->AsString();
sha256_update(&h, str->Bytes(), str->Len());
}
else
{
ODesc d(DESC_BINARY);
v->Describe(&d);
sha256_update(&h, (const u_char *) d.Bytes(), d.Len());
}
}
sha256_final(&h, result);
}
bool SHA256Val::Init()
{
sha256_init(&ctx);

View file

@ -49,6 +49,8 @@ private:
class SHA1Val : public HashVal {
public:
static void digest(val_list& vlist, u_char result[SHA_DIGEST_LENGTH]);
SHA1Val() : HashVal(new OpaqueType("sha1")) { }
protected:
@ -66,6 +68,8 @@ private:
class SHA256Val : public HashVal {
public:
static void digest(val_list& vlist, u_char result[SHA256_DIGEST_LENGTH]);
SHA256Val() : HashVal(new OpaqueType("sha256")) { }
protected:

View file

@ -529,55 +529,6 @@ function piped_exec%(program: string, to_write: string%): bool
return new Val(1, TYPE_BOOL);
%}
%%{
// TODO: Migrate these functions into SHA*Val, in the same vein as MD5Val.
static void hash_sha1_val(val_list& vlist, unsigned char digest[20])
{
SHA_CTX h;
sha1_init(&h);
loop_over_list(vlist, i)
{
Val* v = vlist[i];
if ( v->Type()->Tag() == TYPE_STRING )
{
const BroString* str = v->AsString();
sha1_update(&h, str->Bytes(), str->Len());
}
else
{
ODesc d(DESC_BINARY);
v->Describe(&d);
sha1_update(&h, (const u_char *) d.Bytes(), d.Len());
}
}
sha1_final(&h, digest);
}
static void hash_sha256_val(val_list& vlist, unsigned char digest[32])
{
SHA256_CTX h;
sha256_init(&h);
loop_over_list(vlist, i)
{
Val* v = vlist[i];
if ( v->Type()->Tag() == TYPE_STRING )
{
const BroString* str = v->AsString();
sha256_update(&h, str->Bytes(), str->Len());
}
else
{
ODesc d(DESC_BINARY);
v->Describe(&d);
sha256_update(&h, (const u_char *) d.Bytes(), d.Len());
}
}
sha256_final(&h, digest);
}
%%}
%%{
#include "OpaqueVal.h"
%%}
@ -618,7 +569,7 @@ function md5_hash%(...%): string
function sha1_hash%(...%): string
%{
unsigned char digest[SHA_DIGEST_LENGTH];
hash_sha1_val(@ARG@, digest);
SHA1Val::digest(@ARG@, digest);
return new StringVal(sha1_digest_print(digest));
%}
@ -638,7 +589,7 @@ function sha1_hash%(...%): string
function sha256_hash%(...%): string
%{
unsigned char digest[SHA256_DIGEST_LENGTH];
hash_sha256_val(@ARG@, digest);
SHA256Val::digest(@ARG@, digest);
return new StringVal(sha256_digest_print(digest));
%}
@ -659,7 +610,7 @@ function md5_hmac%(...%): string
%}
## Constructs an MD5 handle to enable incremental hash computation. You can
## feed data to the ## returned opaque value with ## :bro:id:`md5_hash_update`
## feed data to the returned opaque value with ## :bro:id:`md5_hash_update`
## and finally need to call :bro:id:`md5_hash_finish` to finish the computation
## and get the final hash value.
##