commit 6e13f80bcbb891b18c25194519a634da798cf0c9
parent ea42230b00e471500a2bb9731bc32e137a0eee98
Author: Armin Preiml <apreiml@strohwolke.at>
Date: Wed, 17 Nov 2021 12:30:39 +0100
add hash::createfunc
Signed-off-by: Armin Preiml <apreiml@strohwolke.at>
Diffstat:
4 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/crypto/md5/md5.ha b/crypto/md5/md5.ha
@@ -40,6 +40,9 @@ export fn md5() digest = {
return md5;
};
+// [[hash::createfunc]] to allocate a hash state to compute MD5
+export fn create() *hash::hash = alloc(md5());
+
fn write(st: *io::stream, buf: const []u8) (size | io::error) = {
let h = st: *digest;
let b: []u8 = buf;
diff --git a/crypto/sha1/sha1.ha b/crypto/sha1/sha1.ha
@@ -39,6 +39,9 @@ export fn sha1() digest = {
return sha;
};
+// [[hash::createfunc]] to allocate a hash state to compute SHA1
+export fn create() *hash::hash = alloc(sha1());
+
fn write(st: *io::stream, buf: const []u8) (size | io::error) = {
let h = st: *digest;
let b: []u8 = buf;
diff --git a/crypto/sha256/sha256.ha b/crypto/sha256/sha256.ha
@@ -53,6 +53,9 @@ export fn sha256() state = {
return sha;
};
+// [[hash::createfunc]] to allocate a hash state to compute SHA-256
+export fn create() *hash::hash = alloc(sha256());
+
fn reset(h: *hash::hash) void = {
let h = h: *state;
h.h[0] = init0;
diff --git a/hash/hash.ha b/hash/hash.ha
@@ -52,3 +52,8 @@ export fn sz(h: *hash) size = h.sz;
// Returns the block size of the hash.
export fn bsz(h: *hash) size = h.bsz;
+
+// General purpose type for creating hash functions, such as via
+// [[crypto::sha256::create]]. Note that hashes created this way must be freed
+// by the caller.
+export type createfunc = fn() *hash::hash;