aboutsummaryrefslogtreecommitdiff
path: root/lib/std/crypto/md5.zig
diff options
context:
space:
mode:
authorFrank Denis <github@pureftpd.org>2020-08-21 00:51:14 +0200
committerFrank Denis <github@pureftpd.org>2020-08-21 00:51:14 +0200
commitfc55cd458a7cd72ea876cf747b1ddd43d8dc6e20 (patch)
tree421c3e4036ac5288aa9312d4af68608884f8dae1 /lib/std/crypto/md5.zig
parentadf3d00e874275557b69ef65164a2b2a0dd88167 (diff)
downloadzig-fc55cd458a7cd72ea876cf747b1ddd43d8dc6e20.tar.gz
zig-fc55cd458a7cd72ea876cf747b1ddd43d8dc6e20.zip
Hash functions now accept an option set
- This avoids having multiple `init()` functions for every combination of optional parameters - The API is consistent across all hash functions - New options can be added later without breaking existing applications. For example, this is going to come in handy if we implement parallelization for BLAKE2 and BLAKE3. - We don't have a mix of snake_case and camelCase functions any more, at least in the public crypto API Support for BLAKE2 salt and personalization (more commonly called context) parameters have been implemented by the way to illustrate this.
Diffstat (limited to 'lib/std/crypto/md5.zig')
-rw-r--r--lib/std/crypto/md5.zig15
1 files changed, 8 insertions, 7 deletions
diff --git a/lib/std/crypto/md5.zig b/lib/std/crypto/md5.zig
index c33bee2e56..0d221fabf6 100644
--- a/lib/std/crypto/md5.zig
+++ b/lib/std/crypto/md5.zig
@@ -39,6 +39,7 @@ pub const Md5 = struct {
const Self = @This();
pub const block_length = 64;
pub const digest_length = 16;
+ pub const Options = struct {};
s: [4]u32,
// Streaming Cache
@@ -46,7 +47,7 @@ pub const Md5 = struct {
buf_len: u8,
total_len: u64,
- pub fn init() Self {
+ pub fn init(options: Options) Self {
return Self{
.s = [_]u32{
0x67452301,
@@ -60,8 +61,8 @@ pub const Md5 = struct {
};
}
- pub fn hash(b: []const u8, out: []u8) void {
- var d = Md5.init();
+ pub fn hash(b: []const u8, out: []u8, options: Options) void {
+ var d = Md5.init(options);
d.update(b);
d.final(out);
}
@@ -257,18 +258,18 @@ test "md5 single" {
}
test "md5 streaming" {
- var h = Md5.init();
+ var h = Md5.init(.{});
var out: [16]u8 = undefined;
h.final(out[0..]);
htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]);
- h = Md5.init();
+ h = Md5.init(.{});
h.update("abc");
h.final(out[0..]);
htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
- h = Md5.init();
+ h = Md5.init(.{});
h.update("a");
h.update("b");
h.update("c");
@@ -281,7 +282,7 @@ test "md5 aligned final" {
var block = [_]u8{0} ** Md5.block_length;
var out: [Md5.digest_length]u8 = undefined;
- var h = Md5.init();
+ var h = Md5.init(.{});
h.update(&block);
h.final(out[0..]);
}