commit 9ccc6c0b250a0ece928812660dd3a4e09d9d57a2
parent bda1f6e4e8e69fd843b0c6c86bce11ed7e17f0d6
Author: Bor Grošelj Simić <bgs@turminal.net>
Date: Tue, 17 May 2022 21:24:44 +0200
make hash.reset optional
There are hashes where hash.reset doesn't make sense or can even be
harmful in a cryptographic context. This change makes it possible for
hash implementations to omit providing this function.
Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/hash/hash.ha b/hash/hash.ha
@@ -15,7 +15,7 @@ export type hash = struct {
sum: *fn(hash: *hash, buf: []u8) void,
// Resets the hash function to its initial state.
- reset: *fn(hash: *hash) void,
+ reset: nullable *fn(hash: *hash) void,
// Size of the hash in bytes.
sz: size,
@@ -40,7 +40,12 @@ export fn sum(h: *hash, buf: []u8) void = {
};
// Resets the hash function to its initial state.
-export fn reset(h: *hash) void = h.reset(h);
+export fn reset(h: *hash) void = match (h.reset) {
+case let f: *fn(hash: *hash) void =>
+ f(h);
+case null =>
+ abort("this hash cannot be reset");
+};
// Returns the size of the hash in bytes. This is consistent regardless
// of the hash state.