aboutsummaryrefslogtreecommitdiff
path: root/lib/std/crypto/sha3.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/sha3.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/sha3.zig')
-rw-r--r--lib/std/crypto/sha3.zig35
1 files changed, 18 insertions, 17 deletions
diff --git a/lib/std/crypto/sha3.zig b/lib/std/crypto/sha3.zig
index 273e5eea6d..3d6dad1be1 100644
--- a/lib/std/crypto/sha3.zig
+++ b/lib/std/crypto/sha3.zig
@@ -20,17 +20,18 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
const Self = @This();
pub const block_length = 200;
pub const digest_length = bits / 8;
+ pub const Options = struct {};
s: [200]u8,
offset: usize,
rate: usize,
- pub fn init() Self {
+ pub fn init(options: Options) Self {
return Self{ .s = [_]u8{0} ** 200, .offset = 0, .rate = 200 - (bits / 4) };
}
- pub fn hash(b: []const u8, out: []u8) void {
- var d = Self.init();
+ pub fn hash(b: []const u8, out: []u8, options: Options) void {
+ var d = Self.init(options);
d.update(b);
d.final(out);
}
@@ -175,18 +176,18 @@ test "sha3-224 single" {
}
test "sha3-224 streaming" {
- var h = Sha3_224.init();
+ var h = Sha3_224.init(.{});
var out: [28]u8 = undefined;
h.final(out[0..]);
htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]);
- h = Sha3_224.init();
+ h = Sha3_224.init(.{});
h.update("abc");
h.final(out[0..]);
htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]);
- h = Sha3_224.init();
+ h = Sha3_224.init(.{});
h.update("a");
h.update("b");
h.update("c");
@@ -201,18 +202,18 @@ test "sha3-256 single" {
}
test "sha3-256 streaming" {
- var h = Sha3_256.init();
+ var h = Sha3_256.init(.{});
var out: [32]u8 = undefined;
h.final(out[0..]);
htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]);
- h = Sha3_256.init();
+ h = Sha3_256.init(.{});
h.update("abc");
h.final(out[0..]);
htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);
- h = Sha3_256.init();
+ h = Sha3_256.init(.{});
h.update("a");
h.update("b");
h.update("c");
@@ -224,7 +225,7 @@ test "sha3-256 aligned final" {
var block = [_]u8{0} ** Sha3_256.block_length;
var out: [Sha3_256.digest_length]u8 = undefined;
- var h = Sha3_256.init();
+ var h = Sha3_256.init(.{});
h.update(&block);
h.final(out[0..]);
}
@@ -239,7 +240,7 @@ test "sha3-384 single" {
}
test "sha3-384 streaming" {
- var h = Sha3_384.init();
+ var h = Sha3_384.init(.{});
var out: [48]u8 = undefined;
const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
@@ -247,12 +248,12 @@ test "sha3-384 streaming" {
htest.assertEqual(h1, out[0..]);
const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";
- h = Sha3_384.init();
+ h = Sha3_384.init(.{});
h.update("abc");
h.final(out[0..]);
htest.assertEqual(h2, out[0..]);
- h = Sha3_384.init();
+ h = Sha3_384.init(.{});
h.update("a");
h.update("b");
h.update("c");
@@ -270,7 +271,7 @@ test "sha3-512 single" {
}
test "sha3-512 streaming" {
- var h = Sha3_512.init();
+ var h = Sha3_512.init(.{});
var out: [64]u8 = undefined;
const h1 = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";
@@ -278,12 +279,12 @@ test "sha3-512 streaming" {
htest.assertEqual(h1, out[0..]);
const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
- h = Sha3_512.init();
+ h = Sha3_512.init(.{});
h.update("abc");
h.final(out[0..]);
htest.assertEqual(h2, out[0..]);
- h = Sha3_512.init();
+ h = Sha3_512.init(.{});
h.update("a");
h.update("b");
h.update("c");
@@ -295,7 +296,7 @@ test "sha3-512 aligned final" {
var block = [_]u8{0} ** Sha3_512.block_length;
var out: [Sha3_512.digest_length]u8 = undefined;
- var h = Sha3_512.init();
+ var h = Sha3_512.init(.{});
h.update(&block);
h.final(out[0..]);
}