authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-30 17:22:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-30 17:22:42-07:00
log8703053296a10f971ef119369116e6b7ef2e3798
tree3df83f2d7394892cdaef306987282aa9bb2173c6
parent18bc68847107d5bd183df0023b5bb123139f5343

std.crypto: support for hashes without options


26 files changed, 211 insertions(+), 195 deletions(-)

lib/std/Build/Step/Compile.zig+1-2
...@@ -1821,8 +1821,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {...@@ -1821,8 +1821,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1821 const partially_quoted = try std.mem.join(arena, "\" \"", escaped_args.items);1821 const partially_quoted = try std.mem.join(arena, "\" \"", escaped_args.items);
1822 const args = try std.mem.concat(arena, u8, &[_][]const u8{ "\"", partially_quoted, "\"" });1822 const args = try std.mem.concat(arena, u8, &[_][]const u8{ "\"", partially_quoted, "\"" });
18231823
1824 var args_hash: [Sha256.digest_length]u8 = undefined;1824 const args_hash = Sha256.hash(args);
1825 Sha256.hash(args, &args_hash, .{});
1826 var args_hex_hash: [Sha256.digest_length * 2]u8 = undefined;1825 var args_hex_hash: [Sha256.digest_length * 2]u8 = undefined;
1827 _ = try std.fmt.bufPrint(&args_hex_hash, "{x}", .{&args_hash});1826 _ = try std.fmt.bufPrint(&args_hex_hash, "{x}", .{&args_hash});
18281827
lib/std/compress/xz/block.zig+1-2
...@@ -190,8 +190,7 @@ pub fn Decoder(comptime ReaderType: type) type {...@@ -190,8 +190,7 @@ pub fn Decoder(comptime ReaderType: type) type {
190 return error.WrongChecksum;190 return error.WrongChecksum;
191 },191 },
192 .sha256 => {192 .sha256 => {
193 var hash_a: [Sha256.digest_length]u8 = undefined;193 const hash_a = Sha256.hash(unpacked_bytes);
194 Sha256.hash(unpacked_bytes, &hash_a);
195194
196 var hash_b: [Sha256.digest_length]u8 = undefined;195 var hash_b: [Sha256.digest_length]u8 = undefined;
197 try self.inner_reader.readNoEof(&hash_b);196 try self.inner_reader.readNoEof(&hash_b);
lib/std/crypto.zig+1-1
...@@ -368,7 +368,7 @@ test "issue #4532: no index out of bounds" {...@@ -368,7 +368,7 @@ test "issue #4532: no index out of bounds" {
368 var block = [_]u8{'#'} ** Hasher.block_length;368 var block = [_]u8{'#'} ** Hasher.block_length;
369 var out1: [Hasher.digest_length]u8 = undefined;369 var out1: [Hasher.digest_length]u8 = undefined;
370 var out2: [Hasher.digest_length]u8 = undefined;370 var out2: [Hasher.digest_length]u8 = undefined;
371 const h0 = Hasher.init(.{});371 const h0 = Hasher.init();
372 var h = h0;372 var h = h0;
373 h.update(block[0..]);373 h.update(block[0..]);
374 h.final(&out1);374 h.final(&out1);
lib/std/crypto/25519/ed25519.zig+8-8
...@@ -54,7 +54,7 @@ pub const Ed25519 = struct {...@@ -54,7 +54,7 @@ pub const Ed25519 = struct {
54 // Return the clamped secret scalar and prefix for this secret key54 // Return the clamped secret scalar and prefix for this secret key
55 fn scalarAndPrefix(self: SecretKey) struct { scalar: CompressedScalar, prefix: [32]u8 } {55 fn scalarAndPrefix(self: SecretKey) struct { scalar: CompressedScalar, prefix: [32]u8 } {
56 var az: [Sha512.digest_length]u8 = undefined;56 var az: [Sha512.digest_length]u8 = undefined;
57 var h = Sha512.init(.{});57 var h = Sha512.init();
58 h.update(&self.seed());58 h.update(&self.seed());
59 h.final(&az);59 h.final(&az);
6060
...@@ -80,7 +80,7 @@ pub const Ed25519 = struct {...@@ -80,7 +80,7 @@ pub const Ed25519 = struct {
80 var t: [64]u8 = undefined;80 var t: [64]u8 = undefined;
81 t[0..32].* = r_bytes;81 t[0..32].* = r_bytes;
82 t[32..].* = public_key.bytes;82 t[32..].* = public_key.bytes;
83 var h = Sha512.init(.{});83 var h = Sha512.init();
84 h.update(&t);84 h.update(&t);
8585
86 return Signer{ .h = h, .scalar = scalar, .nonce = nonce, .r_bytes = r_bytes };86 return Signer{ .h = h, .scalar = scalar, .nonce = nonce, .r_bytes = r_bytes };
...@@ -128,7 +128,7 @@ pub const Ed25519 = struct {...@@ -128,7 +128,7 @@ pub const Ed25519 = struct {
128 }128 }
129129
130 fn computeNonceAndSign(public_key: PublicKey, msg: []const u8, noise: ?[noise_length]u8, scalar: CompressedScalar, prefix: []const u8) (IdentityElementError || NonCanonicalError || KeyMismatchError || WeakPublicKeyError)!Signature {130 fn computeNonceAndSign(public_key: PublicKey, msg: []const u8, noise: ?[noise_length]u8, scalar: CompressedScalar, prefix: []const u8) (IdentityElementError || NonCanonicalError || KeyMismatchError || WeakPublicKeyError)!Signature {
131 var h = Sha512.init(.{});131 var h = Sha512.init();
132 if (noise) |*z| {132 if (noise) |*z| {
133 h.update(z);133 h.update(z);
134 }134 }
...@@ -257,7 +257,7 @@ pub const Ed25519 = struct {...@@ -257,7 +257,7 @@ pub const Ed25519 = struct {
257 /// from which the actual secret is derived.257 /// from which the actual secret is derived.
258 pub fn generateDeterministic(seed: [seed_length]u8) IdentityElementError!KeyPair {258 pub fn generateDeterministic(seed: [seed_length]u8) IdentityElementError!KeyPair {
259 var az: [Sha512.digest_length]u8 = undefined;259 var az: [Sha512.digest_length]u8 = undefined;
260 var h = Sha512.init(.{});260 var h = Sha512.init();
261 h.update(&seed);261 h.update(&seed);
262 h.final(&az);262 h.final(&az);
263 const pk_p = Curve.basePoint.clampedMul(az[0..32].*) catch return error.IdentityElement;263 const pk_p = Curve.basePoint.clampedMul(az[0..32].*) catch return error.IdentityElement;
...@@ -335,7 +335,7 @@ pub const Ed25519 = struct {...@@ -335,7 +335,7 @@ pub const Ed25519 = struct {
335 return error.KeyMismatch;335 return error.KeyMismatch;
336 }336 }
337 const scalar_and_prefix = key_pair.secret_key.scalarAndPrefix();337 const scalar_and_prefix = key_pair.secret_key.scalarAndPrefix();
338 var h = Sha512.init(.{});338 var h = Sha512.init();
339 h.update(&scalar_and_prefix.prefix);339 h.update(&scalar_and_prefix.prefix);
340 var noise2: [noise_length]u8 = undefined;340 var noise2: [noise_length]u8 = undefined;
341 crypto.random.bytes(&noise2);341 crypto.random.bytes(&noise2);
...@@ -382,7 +382,7 @@ pub const Ed25519 = struct {...@@ -382,7 +382,7 @@ pub const Ed25519 = struct {
382382
383 var hram_batch: [count]Curve.scalar.CompressedScalar = undefined;383 var hram_batch: [count]Curve.scalar.CompressedScalar = undefined;
384 for (signature_batch, 0..) |signature, i| {384 for (signature_batch, 0..) |signature, i| {
385 var h = Sha512.init(.{});385 var h = Sha512.init();
386 h.update(&r_batch[i]);386 h.update(&r_batch[i]);
387 h.update(&signature.public_key.bytes);387 h.update(&signature.public_key.bytes);
388 h.update(signature.msg);388 h.update(signature.msg);
...@@ -452,7 +452,7 @@ pub const Ed25519 = struct {...@@ -452,7 +452,7 @@ pub const Ed25519 = struct {
452 /// Create an blind key pair from an existing key pair, a blinding seed and a context.452 /// Create an blind key pair from an existing key pair, a blinding seed and a context.
453 pub fn init(key_pair: Ed25519.KeyPair, blind_seed: [blind_seed_length]u8, ctx: []const u8) (NonCanonicalError || IdentityElementError)!BlindKeyPair {453 pub fn init(key_pair: Ed25519.KeyPair, blind_seed: [blind_seed_length]u8, ctx: []const u8) (NonCanonicalError || IdentityElementError)!BlindKeyPair {
454 var h: [Sha512.digest_length]u8 = undefined;454 var h: [Sha512.digest_length]u8 = undefined;
455 Sha512.hash(&key_pair.secret_key.seed(), &h, .{});455 h = Sha512.hash(&key_pair.secret_key.seed());
456 Curve.scalar.clamp(h[0..32]);456 Curve.scalar.clamp(h[0..32]);
457 const scalar = Curve.scalar.reduce(h[0..32].*);457 const scalar = Curve.scalar.reduce(h[0..32].*);
458458
...@@ -494,7 +494,7 @@ pub const Ed25519 = struct {...@@ -494,7 +494,7 @@ pub const Ed25519 = struct {
494 /// Compute a blind context from a blinding seed and a context.494 /// Compute a blind context from a blinding seed and a context.
495 fn blindCtx(blind_seed: [blind_seed_length]u8, ctx: []const u8) [Sha512.digest_length]u8 {495 fn blindCtx(blind_seed: [blind_seed_length]u8, ctx: []const u8) [Sha512.digest_length]u8 {
496 var blind_h: [Sha512.digest_length]u8 = undefined;496 var blind_h: [Sha512.digest_length]u8 = undefined;
497 var hx = Sha512.init(.{});497 var hx = Sha512.init();
498 hx.update(&blind_seed);498 hx.update(&blind_seed);
499 hx.update(&[1]u8{0});499 hx.update(&[1]u8{0});
500 hx.update(ctx);500 hx.update(ctx);
lib/std/crypto/25519/edwards25519.zig+3-3
...@@ -467,7 +467,7 @@ pub const Edwards25519 = struct {...@@ -467,7 +467,7 @@ pub const Edwards25519 = struct {
467 var xctx = ctx;467 var xctx = ctx;
468 var hctx: [H.digest_length]u8 = undefined;468 var hctx: [H.digest_length]u8 = undefined;
469 if (ctx.len > 0xff) {469 if (ctx.len > 0xff) {
470 var st = H.init(.{});470 var st = H.init();
471 st.update("H2C-OVERSIZE-DST-");471 st.update("H2C-OVERSIZE-DST-");
472 st.update(ctx);472 st.update(ctx);
473 st.final(&hctx);473 st.final(&hctx);
...@@ -476,7 +476,7 @@ pub const Edwards25519 = struct {...@@ -476,7 +476,7 @@ pub const Edwards25519 = struct {
476 const empty_block = [_]u8{0} ** H.block_length;476 const empty_block = [_]u8{0} ** H.block_length;
477 var t = [3]u8{ 0, n * h_l, 0 };477 var t = [3]u8{ 0, n * h_l, 0 };
478 var xctx_len_u8 = [1]u8{@as(u8, @intCast(xctx.len))};478 var xctx_len_u8 = [1]u8{@as(u8, @intCast(xctx.len))};
479 var st = H.init(.{});479 var st = H.init();
480 st.update(empty_block[0..]);480 st.update(empty_block[0..]);
481 st.update(s);481 st.update(s);
482 st.update(t[0..]);482 st.update(t[0..]);
...@@ -493,7 +493,7 @@ pub const Edwards25519 = struct {...@@ -493,7 +493,7 @@ pub const Edwards25519 = struct {
493 u[i + j] ^= u[i + j - H.digest_length];493 u[i + j] ^= u[i + j - H.digest_length];
494 }494 }
495 t[2] += 1;495 t[2] += 1;
496 st = H.init(.{});496 st = H.init();
497 st.update(u[i..][0..H.digest_length]);497 st.update(u[i..][0..H.digest_length]);
498 st.update(t[2..3]);498 st.update(t[2..3]);
499 st.update(xctx);499 st.update(xctx);
lib/std/crypto/25519/x25519.zig+1-2
...@@ -55,8 +55,7 @@ pub const X25519 = struct {...@@ -55,8 +55,7 @@ pub const X25519 = struct {
55 /// Create a key pair from an Ed25519 key pair55 /// Create a key pair from an Ed25519 key pair
56 pub fn fromEd25519(ed25519_key_pair: crypto.sign.Ed25519.KeyPair) (IdentityElementError || EncodingError)!KeyPair {56 pub fn fromEd25519(ed25519_key_pair: crypto.sign.Ed25519.KeyPair) (IdentityElementError || EncodingError)!KeyPair {
57 const seed = ed25519_key_pair.secret_key.seed();57 const seed = ed25519_key_pair.secret_key.seed();
58 var az: [Sha512.digest_length]u8 = undefined;58 const az = Sha512.hash(&seed);
59 Sha512.hash(&seed, &az, .{});
60 var sk = az[0..32].*;59 var sk = az[0..32].*;
61 Curve.scalar.clamp(&sk);60 Curve.scalar.clamp(&sk);
62 const pk = try publicKeyFromEd25519(ed25519_key_pair.public_key);61 const pk = try publicKeyFromEd25519(ed25519_key_pair.public_key);
lib/std/crypto/Certificate.zig+2-3
...@@ -1037,8 +1037,7 @@ pub const rsa = struct {...@@ -1037,8 +1037,7 @@ pub const rsa = struct {
1037 std.mem.copyForwards(u8, m_p[(8 + Hash.digest_length)..], salt);1037 std.mem.copyForwards(u8, m_p[(8 + Hash.digest_length)..], salt);
10381038
1039 // 13. Let H' = Hash(M'), an octet string of length hLen.1039 // 13. Let H' = Hash(M'), an octet string of length hLen.
1040 var h_p: [Hash.digest_length]u8 = undefined;1040 const h_p = Hash.hash(m_p);
1041 Hash.hash(m_p, &h_p);
10421041
1043 // 14. If H = H', output "consistent". Otherwise, output1042 // 14. If H = H', output "consistent". Otherwise, output
1044 // "inconsistent".1043 // "inconsistent".
...@@ -1054,7 +1053,7 @@ pub const rsa = struct {...@@ -1054,7 +1053,7 @@ pub const rsa = struct {
10541053
1055 while (idx < len) {1054 while (idx < len) {
1056 std.mem.writeInt(u32, hash[seed.len..][0..4], counter, .big);1055 std.mem.writeInt(u32, hash[seed.len..][0..4], counter, .big);
1057 Hash.hash(&hash, out[idx..][0..Hash.digest_length]);1056 out[idx..][0..Hash.digest_length].* = Hash.hash(&hash);
1058 idx += Hash.digest_length;1057 idx += Hash.digest_length;
1059 counter += 1;1058 counter += 1;
1060 }1059 }
lib/std/crypto/Sha1.zig+3-3
...@@ -277,9 +277,9 @@ const RoundParam = struct {...@@ -277,9 +277,9 @@ const RoundParam = struct {
277const htest = @import("test.zig");277const htest = @import("test.zig");
278278
279test "sha1 single" {279test "sha1 single" {
280 try htest.assertEqualHashInterface(Sha1, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "");280 try htest.assertEqualHash(Sha1, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "");
281 try htest.assertEqualHashInterface(Sha1, "a9993e364706816aba3e25717850c26c9cd0d89d", "abc");281 try htest.assertEqualHash(Sha1, "a9993e364706816aba3e25717850c26c9cd0d89d", "abc");
282 try htest.assertEqualHashInterface(Sha1, "a49b2446a02c645bf419f995b67091253a04a259", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");282 try htest.assertEqualHash(Sha1, "a49b2446a02c645bf419f995b67091253a04a259", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
283}283}
284284
285test "sha1 streaming" {285test "sha1 streaming" {
lib/std/crypto/argon2.zig+5-5
...@@ -113,7 +113,7 @@ fn initHash(...@@ -113,7 +113,7 @@ fn initHash(
113 var h0: H0 = undefined;113 var h0: H0 = undefined;
114 var parameters: [24]u8 = undefined;114 var parameters: [24]u8 = undefined;
115 var tmp: [4]u8 = undefined;115 var tmp: [4]u8 = undefined;
116 var b2 = Blake2b512.init(.{});116 var b2 = Blake2b512.init();
117 mem.writeInt(u32, parameters[0..4], params.p, .little);117 mem.writeInt(u32, parameters[0..4], params.p, .little);
118 mem.writeInt(u32, parameters[4..8], @as(u32, @intCast(dk_len)), .little);118 mem.writeInt(u32, parameters[4..8], @as(u32, @intCast(dk_len)), .little);
119 mem.writeInt(u32, parameters[8..12], params.m, .little);119 mem.writeInt(u32, parameters[8..12], params.m, .little);
...@@ -149,7 +149,7 @@ fn blake2bLong(out: []u8, in: []const u8) void {...@@ -149,7 +149,7 @@ fn blake2bLong(out: []u8, in: []const u8) void {
149 var out_buf: [H.digest_length]u8 = undefined;149 var out_buf: [H.digest_length]u8 = undefined;
150150
151 if (out.len <= H.digest_length) {151 if (out.len <= H.digest_length) {
152 var h = H.init(.{ .expected_out_bits = out.len * 8 });152 var h = H.initOptions(.{ .expected_out_bits = out.len * 8 });
153 h.update(&outlen_bytes);153 h.update(&outlen_bytes);
154 h.update(in);154 h.update(in);
155 h.final(&out_buf);155 h.final(&out_buf);
...@@ -157,7 +157,7 @@ fn blake2bLong(out: []u8, in: []const u8) void {...@@ -157,7 +157,7 @@ fn blake2bLong(out: []u8, in: []const u8) void {
157 return;157 return;
158 }158 }
159159
160 var h = H.init(.{});160 var h = H.init();
161 h.update(&outlen_bytes);161 h.update(&outlen_bytes);
162 h.update(in);162 h.update(in);
163 h.final(&out_buf);163 h.final(&out_buf);
...@@ -168,12 +168,12 @@ fn blake2bLong(out: []u8, in: []const u8) void {...@@ -168,12 +168,12 @@ fn blake2bLong(out: []u8, in: []const u8) void {
168 var in_buf: [H.digest_length]u8 = undefined;168 var in_buf: [H.digest_length]u8 = undefined;
169 while (out_slice.len > H.digest_length) {169 while (out_slice.len > H.digest_length) {
170 in_buf = out_buf;170 in_buf = out_buf;
171 H.hash(&in_buf, &out_buf, .{});171 out_buf = H.hash(&in_buf);
172 out_slice[0 .. H.digest_length / 2].* = out_buf[0 .. H.digest_length / 2].*;172 out_slice[0 .. H.digest_length / 2].* = out_buf[0 .. H.digest_length / 2].*;
173 out_slice = out_slice[H.digest_length / 2 ..];173 out_slice = out_slice[H.digest_length / 2 ..];
174 }174 }
175 in_buf = out_buf;175 in_buf = out_buf;
176 H.hash(&in_buf, &out_buf, .{ .expected_out_bits = out_slice.len * 8 });176 out_buf = H.hashOptions(&in_buf, .{ .expected_out_bits = out_slice.len * 8 });
177 @memcpy(out_slice, out_buf[0..out_slice.len]);177 @memcpy(out_slice, out_buf[0..out_slice.len]);
178}178}
179179
lib/std/crypto/bcrypt.zig+4-4
...@@ -499,7 +499,7 @@ const pbkdf_prf = struct {...@@ -499,7 +499,7 @@ const pbkdf_prf = struct {
499499
500 pub fn init(key: []const u8) Self {500 pub fn init(key: []const u8) Self {
501 var self: Self = undefined;501 var self: Self = undefined;
502 self.hasher = Sha512.init(.{});502 self.hasher = Sha512.init();
503 Sha512.hash(key, &self.sha2pass, .{});503 Sha512.hash(key, &self.sha2pass, .{});
504 return self;504 return self;
505 }505 }
...@@ -575,7 +575,7 @@ pub fn opensshKdf(pass: []const u8, salt: []const u8, key: []u8, rounds: u32) !v...@@ -575,7 +575,7 @@ pub fn opensshKdf(pass: []const u8, salt: []const u8, key: []u8, rounds: u32) !v
575 return error.InvalidInput;575 return error.InvalidInput;
576 }576 }
577 var sha2pass: [Sha512.digest_length]u8 = undefined;577 var sha2pass: [Sha512.digest_length]u8 = undefined;
578 Sha512.hash(pass, &sha2pass, .{});578 sha2pass = Sha512.hash(pass);
579 const stride = (key.len + tmp.len - 1) / tmp.len;579 const stride = (key.len + tmp.len - 1) / tmp.len;
580 var amt = (key.len + stride - 1) / stride;580 var amt = (key.len + stride - 1) / stride;
581 if (math.shr(usize, key.len, 32) >= amt) {581 if (math.shr(usize, key.len, 32) >= amt) {
...@@ -587,14 +587,14 @@ pub fn opensshKdf(pass: []const u8, salt: []const u8, key: []u8, rounds: u32) !v...@@ -587,14 +587,14 @@ pub fn opensshKdf(pass: []const u8, salt: []const u8, key: []u8, rounds: u32) !v
587 var count_salt: [4]u8 = undefined;587 var count_salt: [4]u8 = undefined;
588 std.mem.writeInt(u32, count_salt[0..], count, .big);588 std.mem.writeInt(u32, count_salt[0..], count, .big);
589 var sha2salt: [Sha512.digest_length]u8 = undefined;589 var sha2salt: [Sha512.digest_length]u8 = undefined;
590 var h = Sha512.init(.{});590 var h = Sha512.init();
591 h.update(salt);591 h.update(salt);
592 h.update(&count_salt);592 h.update(&count_salt);
593 h.final(&sha2salt);593 h.final(&sha2salt);
594 tmp2 = pbkdf_prf.hash(sha2pass, sha2salt);594 tmp2 = pbkdf_prf.hash(sha2pass, sha2salt);
595 tmp = tmp2;595 tmp = tmp2;
596 for (1..rounds) |_| {596 for (1..rounds) |_| {
597 Sha512.hash(&tmp2, &sha2salt, .{});597 sha2salt = Sha512.hash(&tmp2);
598 tmp2 = pbkdf_prf.hash(sha2pass, sha2salt);598 tmp2 = pbkdf_prf.hash(sha2pass, sha2salt);
599 for (&tmp, tmp2) |*o, t| o.* ^= t;599 for (&tmp, tmp2) |*o, t| o.* ^= t;
600 }600 }
lib/std/crypto/blake2.zig+90-60
...@@ -40,7 +40,12 @@ pub fn Blake2s(comptime out_bits: usize) type {...@@ -40,7 +40,12 @@ pub fn Blake2s(comptime out_bits: usize) type {
40 pub const key_length_min = 0;40 pub const key_length_min = 0;
41 pub const key_length_max = 32;41 pub const key_length_max = 32;
42 pub const key_length = 32; // recommended key length42 pub const key_length = 32; // recommended key length
43 pub const Options = struct { key: ?[]const u8 = null, salt: ?[8]u8 = null, context: ?[8]u8 = null, expected_out_bits: usize = out_bits };43 pub const Options = struct {
44 key: ?[]const u8 = null,
45 salt: ?[8]u8 = null,
46 context: ?[8]u8 = null,
47 expected_out_bits: usize = out_bits,
48 };
4449
45 const iv = [8]u32{50 const iv = [8]u32{
46 0x6A09E667,51 0x6A09E667,
...@@ -72,7 +77,7 @@ pub fn Blake2s(comptime out_bits: usize) type {...@@ -72,7 +77,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
72 buf: [64]u8,77 buf: [64]u8,
73 buf_len: u8,78 buf_len: u8,
7479
75 pub fn init(options: Options) Self {80 pub fn initOptions(options: Options) Self {
76 comptime debug.assert(8 <= out_bits and out_bits <= 256);81 comptime debug.assert(8 <= out_bits and out_bits <= 256);
7782
78 var d: Self = undefined;83 var d: Self = undefined;
...@@ -100,10 +105,20 @@ pub fn Blake2s(comptime out_bits: usize) type {...@@ -100,10 +105,20 @@ pub fn Blake2s(comptime out_bits: usize) type {
100 return d;105 return d;
101 }106 }
102107
103 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {108 pub fn init() Self {
104 var d = Self.init(options);109 return initOptions(.{});
110 }
111
112 pub fn hash(b: []const u8) [digest_length]u8 {
113 return hashOptions(b, .{});
114 }
115
116 pub fn hashOptions(b: []const u8, options: Options) [digest_length]u8 {
117 var out: [digest_length]u8 = undefined;
118 var d = Self.initOptions(options);
105 d.update(b);119 d.update(b);
106 d.final(out);120 d.final(&out);
121 return out;
107 }122 }
108123
109 pub fn update(d: *Self, b: []const u8) void {124 pub fn update(d: *Self, b: []const u8) void {
...@@ -215,7 +230,7 @@ test "blake2s160 single" {...@@ -215,7 +230,7 @@ test "blake2s160 single" {
215}230}
216231
217test "blake2s160 streaming" {232test "blake2s160 streaming" {
218 var h = Blake2s160.init(.{});233 var h = Blake2s160.init();
219 var out: [20]u8 = undefined;234 var out: [20]u8 = undefined;
220235
221 const h1 = "354c9c33f735962418bdacb9479873429c34916f";236 const h1 = "354c9c33f735962418bdacb9479873429c34916f";
...@@ -225,12 +240,12 @@ test "blake2s160 streaming" {...@@ -225,12 +240,12 @@ test "blake2s160 streaming" {
225240
226 const h2 = "5ae3b99be29b01834c3b508521ede60438f8de17";241 const h2 = "5ae3b99be29b01834c3b508521ede60438f8de17";
227242
228 h = Blake2s160.init(.{});243 h = Blake2s160.init();
229 h.update("abc");244 h.update("abc");
230 h.final(out[0..]);245 h.final(out[0..]);
231 try htest.assertEqual(h2, out[0..]);246 try htest.assertEqual(h2, out[0..]);
232247
233 h = Blake2s160.init(.{});248 h = Blake2s160.init();
234 h.update("a");249 h.update("a");
235 h.update("b");250 h.update("b");
236 h.update("c");251 h.update("c");
...@@ -239,26 +254,26 @@ test "blake2s160 streaming" {...@@ -239,26 +254,26 @@ test "blake2s160 streaming" {
239254
240 const h3 = "b60c4dc60e2681e58fbc24e77f07e02c69e72ed0";255 const h3 = "b60c4dc60e2681e58fbc24e77f07e02c69e72ed0";
241256
242 h = Blake2s160.init(.{});257 h = Blake2s160.init();
243 h.update("a" ** 32);258 h.update("a" ** 32);
244 h.update("b" ** 32);259 h.update("b" ** 32);
245 h.final(out[0..]);260 h.final(out[0..]);
246 try htest.assertEqual(h3, out[0..]);261 try htest.assertEqual(h3, out[0..]);
247262
248 h = Blake2s160.init(.{});263 h = Blake2s160.init();
249 h.update("a" ** 32 ++ "b" ** 32);264 h.update("a" ** 32 ++ "b" ** 32);
250 h.final(out[0..]);265 h.final(out[0..]);
251 try htest.assertEqual(h3, out[0..]);266 try htest.assertEqual(h3, out[0..]);
252267
253 const h4 = "4667fd60791a7fe41f939bca646b4529e296bd68";268 const h4 = "4667fd60791a7fe41f939bca646b4529e296bd68";
254269
255 h = Blake2s160.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });270 h = Blake2s160.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
256 h.update("a" ** 32);271 h.update("a" ** 32);
257 h.update("b" ** 32);272 h.update("b" ** 32);
258 h.final(out[0..]);273 h.final(out[0..]);
259 try htest.assertEqual(h4, out[0..]);274 try htest.assertEqual(h4, out[0..]);
260275
261 h = Blake2s160.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });276 h = Blake2s160.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
262 h.update("a" ** 32 ++ "b" ** 32);277 h.update("a" ** 32 ++ "b" ** 32);
263 h.final(out[0..]);278 h.final(out[0..]);
264 try htest.assertEqual(h4, out[0..]);279 try htest.assertEqual(h4, out[0..]);
...@@ -275,7 +290,7 @@ test "comptime blake2s160" {...@@ -275,7 +290,7 @@ test "comptime blake2s160" {
275290
276 try htest.assertEqualHash(Blake2s160, h1, block[0..]);291 try htest.assertEqualHash(Blake2s160, h1, block[0..]);
277292
278 var h = Blake2s160.init(.{});293 var h = Blake2s160.init();
279 h.update(&block);294 h.update(&block);
280 h.final(out[0..]);295 h.final(out[0..]);
281296
...@@ -298,7 +313,7 @@ test "blake2s224 single" {...@@ -298,7 +313,7 @@ test "blake2s224 single" {
298}313}
299314
300test "blake2s224 streaming" {315test "blake2s224 streaming" {
301 var h = Blake2s224.init(.{});316 var h = Blake2s224.init();
302 var out: [28]u8 = undefined;317 var out: [28]u8 = undefined;
303318
304 const h1 = "1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4";319 const h1 = "1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4";
...@@ -308,12 +323,12 @@ test "blake2s224 streaming" {...@@ -308,12 +323,12 @@ test "blake2s224 streaming" {
308323
309 const h2 = "0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55";324 const h2 = "0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55";
310325
311 h = Blake2s224.init(.{});326 h = Blake2s224.init();
312 h.update("abc");327 h.update("abc");
313 h.final(out[0..]);328 h.final(out[0..]);
314 try htest.assertEqual(h2, out[0..]);329 try htest.assertEqual(h2, out[0..]);
315330
316 h = Blake2s224.init(.{});331 h = Blake2s224.init();
317 h.update("a");332 h.update("a");
318 h.update("b");333 h.update("b");
319 h.update("c");334 h.update("c");
...@@ -322,26 +337,26 @@ test "blake2s224 streaming" {...@@ -322,26 +337,26 @@ test "blake2s224 streaming" {
322337
323 const h3 = "557381a78facd2b298640f4e32113e58967d61420af1aa939d0cfe01";338 const h3 = "557381a78facd2b298640f4e32113e58967d61420af1aa939d0cfe01";
324339
325 h = Blake2s224.init(.{});340 h = Blake2s224.init();
326 h.update("a" ** 32);341 h.update("a" ** 32);
327 h.update("b" ** 32);342 h.update("b" ** 32);
328 h.final(out[0..]);343 h.final(out[0..]);
329 try htest.assertEqual(h3, out[0..]);344 try htest.assertEqual(h3, out[0..]);
330345
331 h = Blake2s224.init(.{});346 h = Blake2s224.init();
332 h.update("a" ** 32 ++ "b" ** 32);347 h.update("a" ** 32 ++ "b" ** 32);
333 h.final(out[0..]);348 h.final(out[0..]);
334 try htest.assertEqual(h3, out[0..]);349 try htest.assertEqual(h3, out[0..]);
335350
336 const h4 = "a4d6a9d253441b80e5dfd60a04db169ffab77aec56a2855c402828c3";351 const h4 = "a4d6a9d253441b80e5dfd60a04db169ffab77aec56a2855c402828c3";
337352
338 h = Blake2s224.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });353 h = Blake2s224.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
339 h.update("a" ** 32);354 h.update("a" ** 32);
340 h.update("b" ** 32);355 h.update("b" ** 32);
341 h.final(out[0..]);356 h.final(out[0..]);
342 try htest.assertEqual(h4, out[0..]);357 try htest.assertEqual(h4, out[0..]);
343358
344 h = Blake2s224.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });359 h = Blake2s224.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
345 h.update("a" ** 32 ++ "b" ** 32);360 h.update("a" ** 32 ++ "b" ** 32);
346 h.final(out[0..]);361 h.final(out[0..]);
347 try htest.assertEqual(h4, out[0..]);362 try htest.assertEqual(h4, out[0..]);
...@@ -357,7 +372,7 @@ test "comptime blake2s224" {...@@ -357,7 +372,7 @@ test "comptime blake2s224" {
357372
358 try htest.assertEqualHash(Blake2s224, h1, block[0..]);373 try htest.assertEqualHash(Blake2s224, h1, block[0..]);
359374
360 var h = Blake2s224.init(.{});375 var h = Blake2s224.init();
361 h.update(&block);376 h.update(&block);
362 h.final(out[0..]);377 h.final(out[0..]);
363378
...@@ -380,7 +395,7 @@ test "blake2s256 single" {...@@ -380,7 +395,7 @@ test "blake2s256 single" {
380}395}
381396
382test "blake2s256 streaming" {397test "blake2s256 streaming" {
383 var h = Blake2s256.init(.{});398 var h = Blake2s256.init();
384 var out: [32]u8 = undefined;399 var out: [32]u8 = undefined;
385400
386 const h1 = "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9";401 const h1 = "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9";
...@@ -390,12 +405,12 @@ test "blake2s256 streaming" {...@@ -390,12 +405,12 @@ test "blake2s256 streaming" {
390405
391 const h2 = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982";406 const h2 = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982";
392407
393 h = Blake2s256.init(.{});408 h = Blake2s256.init();
394 h.update("abc");409 h.update("abc");
395 h.final(out[0..]);410 h.final(out[0..]);
396 try htest.assertEqual(h2, out[0..]);411 try htest.assertEqual(h2, out[0..]);
397412
398 h = Blake2s256.init(.{});413 h = Blake2s256.init();
399 h.update("a");414 h.update("a");
400 h.update("b");415 h.update("b");
401 h.update("c");416 h.update("c");
...@@ -404,13 +419,13 @@ test "blake2s256 streaming" {...@@ -404,13 +419,13 @@ test "blake2s256 streaming" {
404419
405 const h3 = "8d8711dade07a6b92b9a3ea1f40bee9b2c53ff3edd2a273dec170b0163568977";420 const h3 = "8d8711dade07a6b92b9a3ea1f40bee9b2c53ff3edd2a273dec170b0163568977";
406421
407 h = Blake2s256.init(.{});422 h = Blake2s256.init();
408 h.update("a" ** 32);423 h.update("a" ** 32);
409 h.update("b" ** 32);424 h.update("b" ** 32);
410 h.final(out[0..]);425 h.final(out[0..]);
411 try htest.assertEqual(h3, out[0..]);426 try htest.assertEqual(h3, out[0..]);
412427
413 h = Blake2s256.init(.{});428 h = Blake2s256.init();
414 h.update("a" ** 32 ++ "b" ** 32);429 h.update("a" ** 32 ++ "b" ** 32);
415 h.final(out[0..]);430 h.final(out[0..]);
416 try htest.assertEqual(h3, out[0..]);431 try htest.assertEqual(h3, out[0..]);
...@@ -422,16 +437,16 @@ test "blake2s256 keyed" {...@@ -422,16 +437,16 @@ test "blake2s256 keyed" {
422 const h1 = "10f918da4d74fab3302e48a5d67d03804b1ec95372a62a0f33b7c9fa28ba1ae6";437 const h1 = "10f918da4d74fab3302e48a5d67d03804b1ec95372a62a0f33b7c9fa28ba1ae6";
423 const key = "secret_key";438 const key = "secret_key";
424439
425 Blake2s256.hash("a" ** 64 ++ "b" ** 64, &out, .{ .key = key });440 out = Blake2s256.hashOptions("a" ** 64 ++ "b" ** 64, .{ .key = key });
426 try htest.assertEqual(h1, out[0..]);441 try htest.assertEqual(h1, out[0..]);
427442
428 var h = Blake2s256.init(.{ .key = key });443 var h = Blake2s256.initOptions(.{ .key = key });
429 h.update("a" ** 64 ++ "b" ** 64);444 h.update("a" ** 64 ++ "b" ** 64);
430 h.final(out[0..]);445 h.final(out[0..]);
431446
432 try htest.assertEqual(h1, out[0..]);447 try htest.assertEqual(h1, out[0..]);
433448
434 h = Blake2s256.init(.{ .key = key });449 h = Blake2s256.initOptions(.{ .key = key });
435 h.update("a" ** 64);450 h.update("a" ** 64);
436 h.update("b" ** 64);451 h.update("b" ** 64);
437 h.final(out[0..]);452 h.final(out[0..]);
...@@ -449,7 +464,7 @@ test "comptime blake2s256" {...@@ -449,7 +464,7 @@ test "comptime blake2s256" {
449464
450 try htest.assertEqualHash(Blake2s256, h1, block[0..]);465 try htest.assertEqualHash(Blake2s256, h1, block[0..]);
451466
452 var h = Blake2s256.init(.{});467 var h = Blake2s256.init();
453 h.update(&block);468 h.update(&block);
454 h.final(out[0..]);469 h.final(out[0..]);
455470
...@@ -474,7 +489,12 @@ pub fn Blake2b(comptime out_bits: usize) type {...@@ -474,7 +489,12 @@ pub fn Blake2b(comptime out_bits: usize) type {
474 pub const key_length_min = 0;489 pub const key_length_min = 0;
475 pub const key_length_max = 64;490 pub const key_length_max = 64;
476 pub const key_length = 32; // recommended key length491 pub const key_length = 32; // recommended key length
477 pub const Options = struct { key: ?[]const u8 = null, salt: ?[16]u8 = null, context: ?[16]u8 = null, expected_out_bits: usize = out_bits };492 pub const Options = struct {
493 key: ?[]const u8 = null,
494 salt: ?[16]u8 = null,
495 context: ?[16]u8 = null,
496 expected_out_bits: usize = out_bits,
497 };
478498
479 const iv = [8]u64{499 const iv = [8]u64{
480 0x6a09e667f3bcc908,500 0x6a09e667f3bcc908,
...@@ -508,7 +528,7 @@ pub fn Blake2b(comptime out_bits: usize) type {...@@ -508,7 +528,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
508 buf: [128]u8,528 buf: [128]u8,
509 buf_len: u8,529 buf_len: u8,
510530
511 pub fn init(options: Options) Self {531 pub fn initOptions(options: Options) Self {
512 comptime debug.assert(8 <= out_bits and out_bits <= 512);532 comptime debug.assert(8 <= out_bits and out_bits <= 512);
513533
514 var d: Self = undefined;534 var d: Self = undefined;
...@@ -536,10 +556,20 @@ pub fn Blake2b(comptime out_bits: usize) type {...@@ -536,10 +556,20 @@ pub fn Blake2b(comptime out_bits: usize) type {
536 return d;556 return d;
537 }557 }
538558
539 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {559 pub fn init() Self {
540 var d = Self.init(options);560 return initOptions(.{});
561 }
562
563 pub fn hash(b: []const u8) [digest_length]u8 {
564 return hashOptions(b, .{});
565 }
566
567 pub fn hashOptions(b: []const u8, options: Options) [digest_length]u8 {
568 var out: [digest_length]u8 = undefined;
569 var d = Self.initOptions(options);
541 d.update(b);570 d.update(b);
542 d.final(out);571 d.final(&out);
572 return out;
543 }573 }
544574
545 pub fn update(d: *Self, b: []const u8) void {575 pub fn update(d: *Self, b: []const u8) void {
...@@ -639,7 +669,7 @@ test "blake2b160 single" {...@@ -639,7 +669,7 @@ test "blake2b160 single" {
639}669}
640670
641test "blake2b160 streaming" {671test "blake2b160 streaming" {
642 var h = Blake2b160.init(.{});672 var h = Blake2b160.init();
643 var out: [20]u8 = undefined;673 var out: [20]u8 = undefined;
644674
645 const h1 = "3345524abf6bbe1809449224b5972c41790b6cf2";675 const h1 = "3345524abf6bbe1809449224b5972c41790b6cf2";
...@@ -649,12 +679,12 @@ test "blake2b160 streaming" {...@@ -649,12 +679,12 @@ test "blake2b160 streaming" {
649679
650 const h2 = "384264f676f39536840523f284921cdc68b6846b";680 const h2 = "384264f676f39536840523f284921cdc68b6846b";
651681
652 h = Blake2b160.init(.{});682 h = Blake2b160.init();
653 h.update("abc");683 h.update("abc");
654 h.final(out[0..]);684 h.final(out[0..]);
655 try htest.assertEqual(h2, out[0..]);685 try htest.assertEqual(h2, out[0..]);
656686
657 h = Blake2b160.init(.{});687 h = Blake2b160.init();
658 h.update("a");688 h.update("a");
659 h.update("b");689 h.update("b");
660 h.update("c");690 h.update("c");
...@@ -663,18 +693,18 @@ test "blake2b160 streaming" {...@@ -663,18 +693,18 @@ test "blake2b160 streaming" {
663693
664 const h3 = "43758f5de1740f651f1ae39de92260fe8bd5a11f";694 const h3 = "43758f5de1740f651f1ae39de92260fe8bd5a11f";
665695
666 h = Blake2b160.init(.{});696 h = Blake2b160.init();
667 h.update("a" ** 64 ++ "b" ** 64);697 h.update("a" ** 64 ++ "b" ** 64);
668 h.final(out[0..]);698 h.final(out[0..]);
669 try htest.assertEqual(h3, out[0..]);699 try htest.assertEqual(h3, out[0..]);
670700
671 h = Blake2b160.init(.{});701 h = Blake2b160.init();
672 h.update("a" ** 64);702 h.update("a" ** 64);
673 h.update("b" ** 64);703 h.update("b" ** 64);
674 h.final(out[0..]);704 h.final(out[0..]);
675 try htest.assertEqual(h3, out[0..]);705 try htest.assertEqual(h3, out[0..]);
676706
677 h = Blake2b160.init(.{});707 h = Blake2b160.init();
678 h.update("a" ** 64);708 h.update("a" ** 64);
679 h.update("b" ** 64);709 h.update("b" ** 64);
680 h.final(out[0..]);710 h.final(out[0..]);
...@@ -682,13 +712,13 @@ test "blake2b160 streaming" {...@@ -682,13 +712,13 @@ test "blake2b160 streaming" {
682712
683 const h4 = "72328f8a8200663752fc302d372b5dd9b49dd8dc";713 const h4 = "72328f8a8200663752fc302d372b5dd9b49dd8dc";
684714
685 h = Blake2b160.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });715 h = Blake2b160.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
686 h.update("a" ** 64);716 h.update("a" ** 64);
687 h.update("b" ** 64);717 h.update("b" ** 64);
688 h.final(out[0..]);718 h.final(out[0..]);
689 try htest.assertEqual(h4, out[0..]);719 try htest.assertEqual(h4, out[0..]);
690720
691 h = Blake2b160.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });721 h = Blake2b160.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
692 h.update("a" ** 64);722 h.update("a" ** 64);
693 h.update("b" ** 64);723 h.update("b" ** 64);
694 h.final(out[0..]);724 h.final(out[0..]);
...@@ -705,7 +735,7 @@ test "comptime blake2b160" {...@@ -705,7 +735,7 @@ test "comptime blake2b160" {
705735
706 try htest.assertEqualHash(Blake2b160, h1, block[0..]);736 try htest.assertEqualHash(Blake2b160, h1, block[0..]);
707737
708 var h = Blake2b160.init(.{});738 var h = Blake2b160.init();
709 h.update(&block);739 h.update(&block);
710 h.final(out[0..]);740 h.final(out[0..]);
711741
...@@ -728,7 +758,7 @@ test "blake2b384 single" {...@@ -728,7 +758,7 @@ test "blake2b384 single" {
728}758}
729759
730test "blake2b384 streaming" {760test "blake2b384 streaming" {
731 var h = Blake2b384.init(.{});761 var h = Blake2b384.init();
732 var out: [48]u8 = undefined;762 var out: [48]u8 = undefined;
733763
734 const h1 = "b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100";764 const h1 = "b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100";
...@@ -738,12 +768,12 @@ test "blake2b384 streaming" {...@@ -738,12 +768,12 @@ test "blake2b384 streaming" {
738768
739 const h2 = "6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30f54ee6c66ba83be64b302d7cba6ce15bb556f4";769 const h2 = "6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30f54ee6c66ba83be64b302d7cba6ce15bb556f4";
740770
741 h = Blake2b384.init(.{});771 h = Blake2b384.init();
742 h.update("abc");772 h.update("abc");
743 h.final(out[0..]);773 h.final(out[0..]);
744 try htest.assertEqual(h2, out[0..]);774 try htest.assertEqual(h2, out[0..]);
745775
746 h = Blake2b384.init(.{});776 h = Blake2b384.init();
747 h.update("a");777 h.update("a");
748 h.update("b");778 h.update("b");
749 h.update("c");779 h.update("c");
...@@ -752,18 +782,18 @@ test "blake2b384 streaming" {...@@ -752,18 +782,18 @@ test "blake2b384 streaming" {
752782
753 const h3 = "b7283f0172fecbbd7eca32ce10d8a6c06b453cb3cf675b33eb4246f0da2bb94a6c0bdd6eec0b5fd71ec4fd51be80bf4c";783 const h3 = "b7283f0172fecbbd7eca32ce10d8a6c06b453cb3cf675b33eb4246f0da2bb94a6c0bdd6eec0b5fd71ec4fd51be80bf4c";
754784
755 h = Blake2b384.init(.{});785 h = Blake2b384.init();
756 h.update("a" ** 64 ++ "b" ** 64);786 h.update("a" ** 64 ++ "b" ** 64);
757 h.final(out[0..]);787 h.final(out[0..]);
758 try htest.assertEqual(h3, out[0..]);788 try htest.assertEqual(h3, out[0..]);
759789
760 h = Blake2b384.init(.{});790 h = Blake2b384.init();
761 h.update("a" ** 64);791 h.update("a" ** 64);
762 h.update("b" ** 64);792 h.update("b" ** 64);
763 h.final(out[0..]);793 h.final(out[0..]);
764 try htest.assertEqual(h3, out[0..]);794 try htest.assertEqual(h3, out[0..]);
765795
766 h = Blake2b384.init(.{});796 h = Blake2b384.init();
767 h.update("a" ** 64);797 h.update("a" ** 64);
768 h.update("b" ** 64);798 h.update("b" ** 64);
769 h.final(out[0..]);799 h.final(out[0..]);
...@@ -771,13 +801,13 @@ test "blake2b384 streaming" {...@@ -771,13 +801,13 @@ test "blake2b384 streaming" {
771801
772 const h4 = "934c48fcb197031c71f583d92f98703510805e72142e0b46f5752d1e971bc86c355d556035613ff7a4154b4de09dac5c";802 const h4 = "934c48fcb197031c71f583d92f98703510805e72142e0b46f5752d1e971bc86c355d556035613ff7a4154b4de09dac5c";
773803
774 h = Blake2b384.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });804 h = Blake2b384.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
775 h.update("a" ** 64);805 h.update("a" ** 64);
776 h.update("b" ** 64);806 h.update("b" ** 64);
777 h.final(out[0..]);807 h.final(out[0..]);
778 try htest.assertEqual(h4, out[0..]);808 try htest.assertEqual(h4, out[0..]);
779809
780 h = Blake2b384.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });810 h = Blake2b384.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
781 h.update("a" ** 64);811 h.update("a" ** 64);
782 h.update("b" ** 64);812 h.update("b" ** 64);
783 h.final(out[0..]);813 h.final(out[0..]);
...@@ -794,7 +824,7 @@ test "comptime blake2b384" {...@@ -794,7 +824,7 @@ test "comptime blake2b384" {
794824
795 try htest.assertEqualHash(Blake2b384, h1, block[0..]);825 try htest.assertEqualHash(Blake2b384, h1, block[0..]);
796826
797 var h = Blake2b384.init(.{});827 var h = Blake2b384.init();
798 h.update(&block);828 h.update(&block);
799 h.final(out[0..]);829 h.final(out[0..]);
800830
...@@ -817,7 +847,7 @@ test "blake2b512 single" {...@@ -817,7 +847,7 @@ test "blake2b512 single" {
817}847}
818848
819test "blake2b512 streaming" {849test "blake2b512 streaming" {
820 var h = Blake2b512.init(.{});850 var h = Blake2b512.init();
821 var out: [64]u8 = undefined;851 var out: [64]u8 = undefined;
822852
823 const h1 = "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";853 const h1 = "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";
...@@ -827,12 +857,12 @@ test "blake2b512 streaming" {...@@ -827,12 +857,12 @@ test "blake2b512 streaming" {
827857
828 const h2 = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";858 const h2 = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
829859
830 h = Blake2b512.init(.{});860 h = Blake2b512.init();
831 h.update("abc");861 h.update("abc");
832 h.final(out[0..]);862 h.final(out[0..]);
833 try htest.assertEqual(h2, out[0..]);863 try htest.assertEqual(h2, out[0..]);
834864
835 h = Blake2b512.init(.{});865 h = Blake2b512.init();
836 h.update("a");866 h.update("a");
837 h.update("b");867 h.update("b");
838 h.update("c");868 h.update("c");
...@@ -841,12 +871,12 @@ test "blake2b512 streaming" {...@@ -841,12 +871,12 @@ test "blake2b512 streaming" {
841871
842 const h3 = "049980af04d6a2cf16b4b49793c3ed7e40732073788806f2c989ebe9547bda0541d63abe298ec8955d08af48ae731f2e8a0bd6d201655a5473b4aa79d211b920";872 const h3 = "049980af04d6a2cf16b4b49793c3ed7e40732073788806f2c989ebe9547bda0541d63abe298ec8955d08af48ae731f2e8a0bd6d201655a5473b4aa79d211b920";
843873
844 h = Blake2b512.init(.{});874 h = Blake2b512.init();
845 h.update("a" ** 64 ++ "b" ** 64);875 h.update("a" ** 64 ++ "b" ** 64);
846 h.final(out[0..]);876 h.final(out[0..]);
847 try htest.assertEqual(h3, out[0..]);877 try htest.assertEqual(h3, out[0..]);
848878
849 h = Blake2b512.init(.{});879 h = Blake2b512.init();
850 h.update("a" ** 64);880 h.update("a" ** 64);
851 h.update("b" ** 64);881 h.update("b" ** 64);
852 h.final(out[0..]);882 h.final(out[0..]);
...@@ -859,7 +889,7 @@ test "blake2b512 keyed" {...@@ -859,7 +889,7 @@ test "blake2b512 keyed" {
859 const h1 = "8a978060ccaf582f388f37454363071ac9a67e3a704585fd879fb8a419a447e389c7c6de790faa20a7a7dccf197de736bc5b40b98a930b36df5bee7555750c4d";889 const h1 = "8a978060ccaf582f388f37454363071ac9a67e3a704585fd879fb8a419a447e389c7c6de790faa20a7a7dccf197de736bc5b40b98a930b36df5bee7555750c4d";
860 const key = "secret_key";890 const key = "secret_key";
861891
862 Blake2b512.hash("a" ** 64 ++ "b" ** 64, &out, .{ .key = key });892 out = Blake2b512.hashOptions("a" ** 64 ++ "b" ** 64, .{ .key = key });
863 try htest.assertEqual(h1, out[0..]);893 try htest.assertEqual(h1, out[0..]);
864894
865 var h = Blake2b512.init(.{ .key = key });895 var h = Blake2b512.init(.{ .key = key });
...@@ -886,7 +916,7 @@ test "comptime blake2b512" {...@@ -886,7 +916,7 @@ test "comptime blake2b512" {
886916
887 try htest.assertEqualHash(Blake2b512, h1, block[0..]);917 try htest.assertEqualHash(Blake2b512, h1, block[0..]);
888918
889 var h = Blake2b512.init(.{});919 var h = Blake2b512.init();
890 h.update(&block);920 h.update(&block);
891 h.final(out[0..]);921 h.final(out[0..]);
892922
lib/std/crypto/ecdsa.zig+1-1
...@@ -200,7 +200,7 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {...@@ -200,7 +200,7 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
200200
201 fn init(secret_key: SecretKey, noise: ?[noise_length]u8) !Signer {201 fn init(secret_key: SecretKey, noise: ?[noise_length]u8) !Signer {
202 return Signer{202 return Signer{
203 .h = Hash.init(.{}),203 .h = Hash.init(),
204 .secret_key = secret_key,204 .secret_key = secret_key,
205 .noise = noise,205 .noise = noise,
206 };206 };
lib/std/crypto/hash_composition.zig+6-3
...@@ -25,14 +25,17 @@ pub fn Composition(comptime H1: type, comptime H2: type) type {...@@ -25,14 +25,17 @@ pub fn Composition(comptime H1: type, comptime H2: type) type {
25 /// Options for both hashes.25 /// Options for both hashes.
26 pub const Options = struct {26 pub const Options = struct {
27 /// Options for H1.27 /// Options for H1.
28 H1: H1.Options = .{},28 H1: if (@hasDecl(H1, "Options")) H1.Options else struct {} = .{},
29 /// Options for H2.29 /// Options for H2.
30 H2: H2.Options = .{},30 H2: if (@hasDecl(H2, "Options")) H2.Options else struct {} = .{},
31 };31 };
3232
33 /// Initialize the hash composition with the given options.33 /// Initialize the hash composition with the given options.
34 pub fn init(options: Options) Self {34 pub fn init(options: Options) Self {
35 return Self{ .H1 = H1.init(options.H1), .H2 = H2.init(options.H2) };35 return .{
36 .H1 = if (@hasDecl(H1, "Options")) H1.init(options.H1) else H1.init(),
37 .H2 = if (@hasDecl(H2, "Options")) H2.init(options.H2) else H2.init(),
38 };
36 }39 }
3740
38 /// Compute H1(H2(b)).41 /// Compute H1(H2(b)).
lib/std/crypto/hmac.zig+1-1
...@@ -37,7 +37,7 @@ pub fn Hmac(comptime Hash: type) type {...@@ -37,7 +37,7 @@ pub fn Hmac(comptime Hash: type) type {
3737
38 // Normalize key length to block size of hash38 // Normalize key length to block size of hash
39 if (key.len > Hash.block_length) {39 if (key.len > Hash.block_length) {
40 Hash.hash(key, scratch[0..mac_length]);40 scratch[0..mac_length].* = Hash.hash(key);
41 @memset(scratch[mac_length..Hash.block_length], 0);41 @memset(scratch[mac_length..Hash.block_length], 0);
42 } else if (key.len < Hash.block_length) {42 } else if (key.len < Hash.block_length) {
43 @memcpy(scratch[0..key.len], key);43 @memcpy(scratch[0..key.len], key);
lib/std/crypto/md5.zig+8-14
...@@ -52,15 +52,9 @@ pub const Md5 = struct {...@@ -52,15 +52,9 @@ pub const Md5 = struct {
52 };52 };
53 }53 }
5454
55 pub fn hash(data: []const u8, out: *[digest_length]u8) void {55 pub fn hash(data: []const u8) [digest_length]u8 {
56 var d = Md5.init();
57 d.update(data);
58 d.final(out);
59 }
60
61 pub fn hashResult(data: []const u8) [digest_length]u8 {
62 var out: [digest_length]u8 = undefined;56 var out: [digest_length]u8 = undefined;
63 var d = Md5.init(.{});57 var d = Md5.init();
64 d.update(data);58 d.update(data);
65 d.final(&out);59 d.final(&out);
66 return out;60 return out;
...@@ -249,22 +243,22 @@ test "single" {...@@ -249,22 +243,22 @@ test "single" {
249}243}
250244
251test "streaming" {245test "streaming" {
252 var h = Md5.init(.{});246 var h = Md5.init();
253 var out: [16]u8 = undefined;247 var out: [16]u8 = undefined;
254248
255 h.final(out[0..]);249 h.final(out[0..]);
256 try htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]);250 try htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]);
257251
258 h = Md5.init(.{});252 h = Md5.init();
259 h.update("abc");253 h.update("abc");
260 h.final(out[0..]);254 h.final(out[0..]);
261 try htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);255 try htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
262256
263 h = Md5.init(.{});257 h = Md5.init();
264 h.update("a");258 h.update("a");
265 h.update("b");259 h.update("b");
266 h.update("c");260 h.update("c");
267 h.final(out[0..]);261 h.final(&out);
268262
269 try htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);263 try htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
270}264}
...@@ -273,7 +267,7 @@ test "aligned final" {...@@ -273,7 +267,7 @@ test "aligned final" {
273 var block = [_]u8{0} ** Md5.block_length;267 var block = [_]u8{0} ** Md5.block_length;
274 var out: [Md5.digest_length]u8 = undefined;268 var out: [Md5.digest_length]u8 = undefined;
275269
276 var h = Md5.init(.{});270 var h = Md5.init();
277 h.update(&block);271 h.update(&block);
278 h.final(out[0..]);272 h.final(&out);
279}273}
lib/std/crypto/ml_kem.zig+9-9
...@@ -254,7 +254,7 @@ fn Kyber(comptime p: Params) type {...@@ -254,7 +254,7 @@ fn Kyber(comptime p: Params) type {
254 @memcpy(&m, &seed);254 @memcpy(&m, &seed);
255 } else {255 } else {
256 // m = H(seed)256 // m = H(seed)
257 sha3.Sha3_256.hash(&seed, &m, .{});257 m = sha3.Sha3_256.hash(&seed);
258 }258 }
259 } else {259 } else {
260 crypto.random.bytes(&m);260 crypto.random.bytes(&m);
...@@ -262,7 +262,7 @@ fn Kyber(comptime p: Params) type {...@@ -262,7 +262,7 @@ fn Kyber(comptime p: Params) type {
262262
263 // (K', r) = G(m ‖ H(pk))263 // (K', r) = G(m ‖ H(pk))
264 var kr: [inner_plaintext_length + h_length]u8 = undefined;264 var kr: [inner_plaintext_length + h_length]u8 = undefined;
265 var g = sha3.Sha3_512.init(.{});265 var g = sha3.Sha3_512.init();
266 g.update(&m);266 g.update(&m);
267 g.update(&pk.hpk);267 g.update(&pk.hpk);
268 g.final(&kr);268 g.final(&kr);
...@@ -277,7 +277,7 @@ fn Kyber(comptime p: Params) type {...@@ -277,7 +277,7 @@ fn Kyber(comptime p: Params) type {
277 };277 };
278 } else {278 } else {
279 // Compute H(c) and put in second slot of kr, which will be (K', H(c)).279 // Compute H(c) and put in second slot of kr, which will be (K', H(c)).
280 sha3.Sha3_256.hash(&ct, kr[32..], .{});280 kr[32..].* = sha3.Sha3_256.hash(&ct);
281281
282 var ss: [shared_length]u8 = undefined;282 var ss: [shared_length]u8 = undefined;
283 sha3.Shake256.hash(&kr, &ss, .{});283 sha3.Shake256.hash(&kr, &ss, .{});
...@@ -297,7 +297,7 @@ fn Kyber(comptime p: Params) type {...@@ -297,7 +297,7 @@ fn Kyber(comptime p: Params) type {
297 pub fn fromBytes(buf: *const [bytes_length]u8) errors.NonCanonicalError!PublicKey {297 pub fn fromBytes(buf: *const [bytes_length]u8) errors.NonCanonicalError!PublicKey {
298 var ret: PublicKey = undefined;298 var ret: PublicKey = undefined;
299 ret.pk = try InnerPk.fromBytes(buf[0..InnerPk.bytes_length]);299 ret.pk = try InnerPk.fromBytes(buf[0..InnerPk.bytes_length]);
300 sha3.Sha3_256.hash(buf, &ret.hpk, .{});300 ret.hpk = sha3.Sha3_256.hash(buf);
301 return ret;301 return ret;
302 }302 }
303 };303 };
...@@ -320,7 +320,7 @@ fn Kyber(comptime p: Params) type {...@@ -320,7 +320,7 @@ fn Kyber(comptime p: Params) type {
320320
321 // (K'', r') = G(m' ‖ H(pk))321 // (K'', r') = G(m' ‖ H(pk))
322 var kr2: [64]u8 = undefined;322 var kr2: [64]u8 = undefined;
323 var g = sha3.Sha3_512.init(.{});323 var g = sha3.Sha3_512.init();
324 g.update(&m2);324 g.update(&m2);
325 g.update(&sk.hpk);325 g.update(&sk.hpk);
326 g.final(&kr2);326 g.final(&kr2);
...@@ -329,7 +329,7 @@ fn Kyber(comptime p: Params) type {...@@ -329,7 +329,7 @@ fn Kyber(comptime p: Params) type {
329 const ct2 = sk.pk.encrypt(&m2, kr2[32..64]);329 const ct2 = sk.pk.encrypt(&m2, kr2[32..64]);
330330
331 // Compute H(ct) and put in the second slot of kr2 which will be (K'', H(ct)).331 // Compute H(ct) and put in the second slot of kr2 which will be (K'', H(ct)).
332 sha3.Sha3_256.hash(ct, kr2[32..], .{});332 kr2[32..].* = sha3.Sha3_256.hash(ct);
333333
334 // Replace K'' by z in the first slot of kr2 if ct ≠ ct'.334 // Replace K'' by z in the first slot of kr2 if ct ≠ ct'.
335 cmov(32, kr2[0..32], sk.z, ctneq(ciphertext_length, ct.*, ct2));335 cmov(32, kr2[0..32], sk.z, ctneq(ciphertext_length, ct.*, ct2));
...@@ -389,7 +389,7 @@ fn Kyber(comptime p: Params) type {...@@ -389,7 +389,7 @@ fn Kyber(comptime p: Params) type {
389 ret.secret_key.z = seed[inner_seed_length..seed_length].*;389 ret.secret_key.z = seed[inner_seed_length..seed_length].*;
390390
391 // Compute H(pk)391 // Compute H(pk)
392 sha3.Sha3_256.hash(&ret.public_key.pk.toBytes(), &ret.secret_key.hpk, .{});392 ret.secret_key.hpk = sha3.Sha3_256.hash(&ret.public_key.pk.toBytes());
393 ret.public_key.hpk = ret.secret_key.hpk;393 ret.public_key.hpk = ret.secret_key.hpk;
394394
395 return ret;395 return ret;
...@@ -505,7 +505,7 @@ fn Kyber(comptime p: Params) type {...@@ -505,7 +505,7 @@ fn Kyber(comptime p: Params) type {
505 // Derives inner PKE keypair from given seed.505 // Derives inner PKE keypair from given seed.
506 fn innerKeyFromSeed(seed: [inner_seed_length]u8, pk: *InnerPk, sk: *InnerSk) void {506 fn innerKeyFromSeed(seed: [inner_seed_length]u8, pk: *InnerPk, sk: *InnerSk) void {
507 var expanded_seed: [64]u8 = undefined;507 var expanded_seed: [64]u8 = undefined;
508 var h = sha3.Sha3_512.init(.{});508 var h = sha3.Sha3_512.init();
509 if (p.ml_kem) h.update(&[1]u8{p.k});509 if (p.ml_kem) h.update(&[1]u8{p.k});
510 h.update(&seed);510 h.update(&seed);
511 h.final(&expanded_seed);511 h.final(&expanded_seed);
...@@ -1734,7 +1734,7 @@ test "NIST KAT test" {...@@ -1734,7 +1734,7 @@ test "NIST KAT test" {
1734 for (&seed, 0..) |*s, i| {1734 for (&seed, 0..) |*s, i| {
1735 s.* = @as(u8, @intCast(i));1735 s.* = @as(u8, @intCast(i));
1736 }1736 }
1737 var f = sha2.Sha256.init(.{});1737 var f = sha2.Sha256.init();
1738 const fw = f.writer();1738 const fw = f.writer();
1739 var g = NistDRBG.init(seed);1739 var g = NistDRBG.init(seed);
1740 try std.fmt.format(fw, "# {s}\n\n", .{mode.name});1740 try std.fmt.format(fw, "# {s}\n\n", .{mode.name});
lib/std/crypto/salsa20.zig+1-1
...@@ -523,7 +523,7 @@ pub const SealedBox = struct {...@@ -523,7 +523,7 @@ pub const SealedBox = struct {
523 pub const KeyPair = Box.KeyPair;523 pub const KeyPair = Box.KeyPair;
524524
525 fn createNonce(pk1: [public_length]u8, pk2: [public_length]u8) [Box.nonce_length]u8 {525 fn createNonce(pk1: [public_length]u8, pk2: [public_length]u8) [Box.nonce_length]u8 {
526 var hasher = Blake2b(Box.nonce_length * 8).init(.{});526 var hasher = Blake2b(Box.nonce_length * 8).init();
527 hasher.update(&pk1);527 hasher.update(&pk1);
528 hasher.update(&pk2);528 hasher.update(&pk2);
529 var nonce: [Box.nonce_length]u8 = undefined;529 var nonce: [Box.nonce_length]u8 = undefined;
lib/std/crypto/sha2.zig+30-30
...@@ -92,7 +92,6 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {...@@ -92,7 +92,6 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {
92 const Self = @This();92 const Self = @This();
93 pub const block_length = 64;93 pub const block_length = 64;
94 pub const digest_length = digest_bits / 8;94 pub const digest_length = digest_bits / 8;
95 pub const Options = struct {};
9695
97 s: [8]u32 align(16),96 s: [8]u32 align(16),
98 // Streaming Cache97 // Streaming Cache
...@@ -100,15 +99,16 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {...@@ -100,15 +99,16 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {
100 buf_len: u8 = 0,99 buf_len: u8 = 0,
101 total_len: u64 = 0,100 total_len: u64 = 0,
102101
103 pub fn init(options: Options) Self {102 pub fn init() Self {
104 _ = options;
105 return Self{ .s = iv };103 return Self{ .s = iv };
106 }104 }
107105
108 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {106 pub fn hash(b: []const u8) [digest_length]u8 {
109 var d = Self.init(options);107 var out: [digest_length]u8 = undefined;
108 var d = Self.init();
110 d.update(b);109 d.update(b);
111 d.final(out);110 d.final(&out);
111 return out;
112 }112 }
113113
114 pub fn update(d: *Self, b: []const u8) void {114 pub fn update(d: *Self, b: []const u8) void {
...@@ -421,18 +421,18 @@ test Sha224 {...@@ -421,18 +421,18 @@ test Sha224 {
421}421}
422422
423test "sha224 streaming" {423test "sha224 streaming" {
424 var h = Sha224.init(.{});424 var h = Sha224.init();
425 var out: [28]u8 = undefined;425 var out: [28]u8 = undefined;
426426
427 h.final(out[0..]);427 h.final(out[0..]);
428 try htest.assertEqual("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", out[0..]);428 try htest.assertEqual("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", out[0..]);
429429
430 h = Sha224.init(.{});430 h = Sha224.init();
431 h.update("abc");431 h.update("abc");
432 h.final(out[0..]);432 h.final(out[0..]);
433 try htest.assertEqual("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", out[0..]);433 try htest.assertEqual("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", out[0..]);
434434
435 h = Sha224.init(.{});435 h = Sha224.init();
436 h.update("a");436 h.update("a");
437 h.update("b");437 h.update("b");
438 h.update("c");438 h.update("c");
...@@ -453,18 +453,18 @@ test Sha256T192 {...@@ -453,18 +453,18 @@ test Sha256T192 {
453}453}
454454
455test "sha256 streaming" {455test "sha256 streaming" {
456 var h = Sha256.init(.{});456 var h = Sha256.init();
457 var out: [32]u8 = undefined;457 var out: [32]u8 = undefined;
458458
459 h.final(out[0..]);459 h.final(out[0..]);
460 try htest.assertEqual("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", out[0..]);460 try htest.assertEqual("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", out[0..]);
461461
462 h = Sha256.init(.{});462 h = Sha256.init();
463 h.update("abc");463 h.update("abc");
464 h.final(out[0..]);464 h.final(out[0..]);
465 try htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);465 try htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);
466466
467 h = Sha256.init(.{});467 h = Sha256.init();
468 h.update("a");468 h.update("a");
469 h.update("b");469 h.update("b");
470 h.update("c");470 h.update("c");
...@@ -473,12 +473,12 @@ test "sha256 streaming" {...@@ -473,12 +473,12 @@ test "sha256 streaming" {
473}473}
474474
475test "sha256 aligned final" {475test "sha256 aligned final" {
476 var block = [_]u8{0} ** Sha256.block_length;476 var block: [Sha256.block_length]u8 = @splat(0);
477 var out: [Sha256.digest_length]u8 = undefined;477 var out: [Sha256.digest_length]u8 = undefined;
478478
479 var h = Sha256.init(.{});479 var h = Sha256.init();
480 h.update(&block);480 h.update(&block);
481 h.final(out[0..]);481 h.final(&out);
482}482}
483483
484const Iv64 = [8]u64;484const Iv64 = [8]u64;
...@@ -487,7 +487,6 @@ fn Sha2x64(comptime iv: Iv64, digest_bits: comptime_int) type {...@@ -487,7 +487,6 @@ fn Sha2x64(comptime iv: Iv64, digest_bits: comptime_int) type {
487 const Self = @This();487 const Self = @This();
488 pub const block_length = 128;488 pub const block_length = 128;
489 pub const digest_length = std.math.divCeil(comptime_int, digest_bits, 8) catch unreachable;489 pub const digest_length = std.math.divCeil(comptime_int, digest_bits, 8) catch unreachable;
490 pub const Options = struct {};
491490
492 s: Iv64,491 s: Iv64,
493 // Streaming Cache492 // Streaming Cache
...@@ -495,15 +494,16 @@ fn Sha2x64(comptime iv: Iv64, digest_bits: comptime_int) type {...@@ -495,15 +494,16 @@ fn Sha2x64(comptime iv: Iv64, digest_bits: comptime_int) type {
495 buf_len: u8 = 0,494 buf_len: u8 = 0,
496 total_len: u128 = 0,495 total_len: u128 = 0,
497496
498 pub fn init(options: Options) Self {497 pub fn init() Self {
499 _ = options;498 return .{ .s = iv };
500 return Self{ .s = iv };
501 }499 }
502500
503 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {501 pub fn hash(b: []const u8) [digest_length]u8 {
504 var d = Self.init(options);502 var out: [digest_length]u8 = undefined;
503 var d = Self.init();
505 d.update(b);504 d.update(b);
506 d.final(out);505 d.final(&out);
506 return out;
507 }507 }
508508
509 pub fn update(d: *Self, b: []const u8) void {509 pub fn update(d: *Self, b: []const u8) void {
...@@ -737,7 +737,7 @@ fn truncatedSha512Iv(digest_len: comptime_int) Iv64 {...@@ -737,7 +737,7 @@ fn truncatedSha512Iv(digest_len: comptime_int) Iv64 {
737737
738 var params: [@sizeOf(Iv64)]u8 = undefined;738 var params: [@sizeOf(Iv64)]u8 = undefined;
739 const algo_str = std.fmt.comptimePrint("SHA-512/{d}", .{digest_len});739 const algo_str = std.fmt.comptimePrint("SHA-512/{d}", .{digest_len});
740 GenHash.hash(algo_str, &params, .{});740 params = GenHash.hash(algo_str);
741741
742 return Iv64{742 return Iv64{
743 std.mem.readInt(u64, params[0..8], .big),743 std.mem.readInt(u64, params[0..8], .big),
...@@ -788,7 +788,7 @@ test Sha384 {...@@ -788,7 +788,7 @@ test Sha384 {
788}788}
789789
790test "sha384 streaming" {790test "sha384 streaming" {
791 var h = Sha384.init(.{});791 var h = Sha384.init();
792 var out: [48]u8 = undefined;792 var out: [48]u8 = undefined;
793793
794 const h1 = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";794 const h1 = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";
...@@ -797,12 +797,12 @@ test "sha384 streaming" {...@@ -797,12 +797,12 @@ test "sha384 streaming" {
797797
798 const h2 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";798 const h2 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";
799799
800 h = Sha384.init(.{});800 h = Sha384.init();
801 h.update("abc");801 h.update("abc");
802 h.final(out[0..]);802 h.final(out[0..]);
803 try htest.assertEqual(h2, out[0..]);803 try htest.assertEqual(h2, out[0..]);
804804
805 h = Sha384.init(.{});805 h = Sha384.init();
806 h.update("a");806 h.update("a");
807 h.update("b");807 h.update("b");
808 h.update("c");808 h.update("c");
...@@ -822,7 +822,7 @@ test Sha512 {...@@ -822,7 +822,7 @@ test Sha512 {
822}822}
823823
824test "sha512 streaming" {824test "sha512 streaming" {
825 var h = Sha512.init(.{});825 var h = Sha512.init();
826 var out: [64]u8 = undefined;826 var out: [64]u8 = undefined;
827827
828 const h1 = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";828 const h1 = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
...@@ -831,12 +831,12 @@ test "sha512 streaming" {...@@ -831,12 +831,12 @@ test "sha512 streaming" {
831831
832 const h2 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";832 const h2 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
833833
834 h = Sha512.init(.{});834 h = Sha512.init();
835 h.update("abc");835 h.update("abc");
836 h.final(out[0..]);836 h.final(out[0..]);
837 try htest.assertEqual(h2, out[0..]);837 try htest.assertEqual(h2, out[0..]);
838838
839 h = Sha512.init(.{});839 h = Sha512.init();
840 h.update("a");840 h.update("a");
841 h.update("b");841 h.update("b");
842 h.update("c");842 h.update("c");
...@@ -848,7 +848,7 @@ test "sha512 aligned final" {...@@ -848,7 +848,7 @@ test "sha512 aligned final" {
848 var block = [_]u8{0} ** Sha512.block_length;848 var block = [_]u8{0} ** Sha512.block_length;
849 var out: [Sha512.digest_length]u8 = undefined;849 var out: [Sha512.digest_length]u8 = undefined;
850850
851 var h = Sha512.init(.{});851 var h = Sha512.init();
852 h.update(&block);852 h.update(&block);
853 h.final(out[0..]);853 h.final(out[0..]);
854}854}
lib/std/crypto/sha3.zig+30-20
...@@ -59,15 +59,25 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim...@@ -59,15 +59,25 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim
59 pub const Options = struct { delim: u8 = default_delim };59 pub const Options = struct { delim: u8 = default_delim };
6060
61 /// Initialize a Keccak hash function.61 /// Initialize a Keccak hash function.
62 pub fn init(options: Options) Self {62 pub fn initOptions(options: Options) Self {
63 return Self{ .st = .{ .delim = options.delim } };63 return Self{ .st = .{ .delim = options.delim } };
64 }64 }
6565
66 pub fn init() Self {
67 return initOptions(.{});
68 }
69
70 pub fn hash(bytes: []const u8) [digest_length]u8 {
71 return hashOptions(bytes, .{});
72 }
73
66 /// Hash a slice of bytes.74 /// Hash a slice of bytes.
67 pub fn hash(bytes: []const u8, out: *[digest_length]u8, options: Options) void {75 pub fn hashOptions(bytes: []const u8, options: Options) [digest_length]u8 {
68 var st = Self.init(options);76 var out: [digest_length]u8 = undefined;
77 var st = Self.initOptions(options);
69 st.update(bytes);78 st.update(bytes);
70 st.final(out);79 st.final(&out);
80 return out;
71 }81 }
7282
73 /// Absorb a slice of bytes into the state.83 /// Absorb a slice of bytes into the state.
...@@ -547,18 +557,18 @@ test "sha3-224 single" {...@@ -547,18 +557,18 @@ test "sha3-224 single" {
547}557}
548558
549test "sha3-224 streaming" {559test "sha3-224 streaming" {
550 var h = Sha3_224.init(.{});560 var h = Sha3_224.init();
551 var out: [28]u8 = undefined;561 var out: [28]u8 = undefined;
552562
553 h.final(out[0..]);563 h.final(out[0..]);
554 try htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]);564 try htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]);
555565
556 h = Sha3_224.init(.{});566 h = Sha3_224.init();
557 h.update("abc");567 h.update("abc");
558 h.final(out[0..]);568 h.final(out[0..]);
559 try htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]);569 try htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]);
560570
561 h = Sha3_224.init(.{});571 h = Sha3_224.init();
562 h.update("a");572 h.update("a");
563 h.update("b");573 h.update("b");
564 h.update("c");574 h.update("c");
...@@ -573,18 +583,18 @@ test "sha3-256 single" {...@@ -573,18 +583,18 @@ test "sha3-256 single" {
573}583}
574584
575test "sha3-256 streaming" {585test "sha3-256 streaming" {
576 var h = Sha3_256.init(.{});586 var h = Sha3_256.init();
577 var out: [32]u8 = undefined;587 var out: [32]u8 = undefined;
578588
579 h.final(out[0..]);589 h.final(out[0..]);
580 try htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]);590 try htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]);
581591
582 h = Sha3_256.init(.{});592 h = Sha3_256.init();
583 h.update("abc");593 h.update("abc");
584 h.final(out[0..]);594 h.final(out[0..]);
585 try htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);595 try htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);
586596
587 h = Sha3_256.init(.{});597 h = Sha3_256.init();
588 h.update("a");598 h.update("a");
589 h.update("b");599 h.update("b");
590 h.update("c");600 h.update("c");
...@@ -596,7 +606,7 @@ test "sha3-256 aligned final" {...@@ -596,7 +606,7 @@ test "sha3-256 aligned final" {
596 var block = [_]u8{0} ** Sha3_256.block_length;606 var block = [_]u8{0} ** Sha3_256.block_length;
597 var out: [Sha3_256.digest_length]u8 = undefined;607 var out: [Sha3_256.digest_length]u8 = undefined;
598608
599 var h = Sha3_256.init(.{});609 var h = Sha3_256.init();
600 h.update(&block);610 h.update(&block);
601 h.final(out[0..]);611 h.final(out[0..]);
602}612}
...@@ -611,7 +621,7 @@ test "sha3-384 single" {...@@ -611,7 +621,7 @@ test "sha3-384 single" {
611}621}
612622
613test "sha3-384 streaming" {623test "sha3-384 streaming" {
614 var h = Sha3_384.init(.{});624 var h = Sha3_384.init();
615 var out: [48]u8 = undefined;625 var out: [48]u8 = undefined;
616626
617 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";627 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
...@@ -619,12 +629,12 @@ test "sha3-384 streaming" {...@@ -619,12 +629,12 @@ test "sha3-384 streaming" {
619 try htest.assertEqual(h1, out[0..]);629 try htest.assertEqual(h1, out[0..]);
620630
621 const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";631 const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";
622 h = Sha3_384.init(.{});632 h = Sha3_384.init();
623 h.update("abc");633 h.update("abc");
624 h.final(out[0..]);634 h.final(out[0..]);
625 try htest.assertEqual(h2, out[0..]);635 try htest.assertEqual(h2, out[0..]);
626636
627 h = Sha3_384.init(.{});637 h = Sha3_384.init();
628 h.update("a");638 h.update("a");
629 h.update("b");639 h.update("b");
630 h.update("c");640 h.update("c");
...@@ -642,7 +652,7 @@ test "sha3-512 single" {...@@ -642,7 +652,7 @@ test "sha3-512 single" {
642}652}
643653
644test "sha3-512 streaming" {654test "sha3-512 streaming" {
645 var h = Sha3_512.init(.{});655 var h = Sha3_512.init();
646 var out: [64]u8 = undefined;656 var out: [64]u8 = undefined;
647657
648 const h1 = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";658 const h1 = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";
...@@ -650,12 +660,12 @@ test "sha3-512 streaming" {...@@ -650,12 +660,12 @@ test "sha3-512 streaming" {
650 try htest.assertEqual(h1, out[0..]);660 try htest.assertEqual(h1, out[0..]);
651661
652 const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";662 const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
653 h = Sha3_512.init(.{});663 h = Sha3_512.init();
654 h.update("abc");664 h.update("abc");
655 h.final(out[0..]);665 h.final(out[0..]);
656 try htest.assertEqual(h2, out[0..]);666 try htest.assertEqual(h2, out[0..]);
657667
658 h = Sha3_512.init(.{});668 h = Sha3_512.init();
659 h.update("a");669 h.update("a");
660 h.update("b");670 h.update("b");
661 h.update("c");671 h.update("c");
...@@ -667,7 +677,7 @@ test "sha3-512 aligned final" {...@@ -667,7 +677,7 @@ test "sha3-512 aligned final" {
667 var block = [_]u8{0} ** Sha3_512.block_length;677 var block = [_]u8{0} ** Sha3_512.block_length;
668 var out: [Sha3_512.digest_length]u8 = undefined;678 var out: [Sha3_512.digest_length]u8 = undefined;
669679
670 var h = Sha3_512.init(.{});680 var h = Sha3_512.init();
671 h.update(&block);681 h.update(&block);
672 h.final(out[0..]);682 h.final(out[0..]);
673}683}
...@@ -730,10 +740,10 @@ test "SHA-3 with streaming" {...@@ -730,10 +740,10 @@ test "SHA-3 with streaming" {
730 var msg: [613]u8 = [613]u8{ 0x97, 0xd1, 0x2d, 0x1a, 0x16, 0x2d, 0x36, 0x4d, 0x20, 0x62, 0x19, 0x0b, 0x14, 0x93, 0xbb, 0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c, 0xe3, 0x2b, 0x70, 0x60, 0x7e, 0x8c, 0xe4, 0xdd, 0x27, 0x30, 0x7e, 0x0d, 0xc7, 0xb7, 0x9a, 0x1a, 0x3c, 0xcc, 0xa7, 0x22, 0x77, 0x14, 0x05, 0x50, 0x57, 0x31, 0x1b, 0xc8, 0xbf, 0xce, 0x52, 0xaf, 0x9c, 0x8e, 0x10, 0x2e, 0xd2, 0x16, 0xb6, 0x6e, 0x43, 0x10, 0xaf, 0x8b, 0xde, 0x1d, 0x60, 0xb2, 0x7d, 0xe6, 0x2f, 0x08, 0x10, 0x12, 0x7e, 0xb4, 0x76, 0x45, 0xb6, 0xd8, 0x9b, 0x26, 0x40, 0xa1, 0x63, 0x5c, 0x7a, 0x2a, 0xb1, 0x8c, 0xd6, 0xa4, 0x6f, 0x5a, 0xae, 0x33, 0x7e, 0x6d, 0x71, 0xf5, 0xc8, 0x6d, 0x80, 0x1c, 0x35, 0xfc, 0x3f, 0xc1, 0xa6, 0xc6, 0x1a, 0x15, 0x04, 0x6d, 0x76, 0x38, 0x32, 0x95, 0xb2, 0x51, 0x1a, 0xe9, 0x3e, 0x89, 0x9f, 0x0c, 0x79 };
731 var out: [Sha3_256.digest_length]u8 = undefined;741 var out: [Sha3_256.digest_length]u8 = undefined;
732742
733 Sha3_256.hash(&msg, &out, .{});743 out = Sha3_256.hash(&msg);
734 try htest.assertEqual("5780048dfa381a1d01c747906e4a08711dd34fd712ecd7c6801dd2b38fd81a89", &out);744 try htest.assertEqual("5780048dfa381a1d01c747906e4a08711dd34fd712ecd7c6801dd2b38fd81a89", &out);
735745
736 var h = Sha3_256.init(.{});746 var h = Sha3_256.init();
737 h.update(msg[0..64]);747 h.update(msg[0..64]);
738 h.update(msg[64..613]);748 h.update(msg[64..613]);
739 h.final(&out);749 h.final(&out);
lib/std/crypto/test.zig-10
...@@ -7,16 +7,6 @@ pub fn assertEqualHash(...@@ -7,16 +7,6 @@ pub fn assertEqualHash(
7 comptime Hasher: type,7 comptime Hasher: type,
8 expected_hex: *const [Hasher.digest_length * 2:0]u8,8 expected_hex: *const [Hasher.digest_length * 2:0]u8,
9 input: []const u8,9 input: []const u8,
10) !void {
11 var h: [Hasher.digest_length]u8 = undefined;
12 Hasher.hash(input, &h, .{});
13 try assertEqual(expected_hex, &h);
14}
15
16pub fn assertEqualHashInterface(
17 comptime Hasher: type,
18 expected_hex: *const [Hasher.digest_length * 2:0]u8,
19 input: []const u8,
20) !void {10) !void {
21 const digest = Hasher.hash(input);11 const digest = Hasher.hash(input);
22 try assertEqual(expected_hex, &digest);12 try assertEqual(expected_hex, &digest);
lib/std/crypto/tls.zig-6
...@@ -576,12 +576,6 @@ pub fn hkdfExpandLabel(...@@ -576,12 +576,6 @@ pub fn hkdfExpandLabel(
576 return result;576 return result;
577}577}
578578
579pub fn emptyHash(comptime Hash: type) [Hash.digest_length]u8 {
580 var result: [Hash.digest_length]u8 = undefined;
581 Hash.hash(&.{}, &result);
582 return result;
583}
584
585pub fn hmac(comptime Hmac: type, message: []const u8, key: [Hmac.key_length]u8) [Hmac.mac_length]u8 {579pub fn hmac(comptime Hmac: type, message: []const u8, key: [Hmac.key_length]u8) [Hmac.mac_length]u8 {
586 var result: [Hmac.mac_length]u8 = undefined;580 var result: [Hmac.mac_length]u8 = undefined;
587 Hmac.create(&result, message, &key);581 Hmac.create(&result, message, &key);
lib/std/crypto/tls/Client.zig+1-1
...@@ -524,7 +524,7 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client...@@ -524,7 +524,7 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
524 const hello_hash = p.transcript_hash.peek();524 const hello_hash = p.transcript_hash.peek();
525 const zeroes = [1]u8{0} ** P.Hash.digest_length;525 const zeroes = [1]u8{0} ** P.Hash.digest_length;
526 const early_secret = P.Hkdf.extract(&[1]u8{0}, &zeroes);526 const early_secret = P.Hkdf.extract(&[1]u8{0}, &zeroes);
527 const empty_hash = tls.emptyHash(P.Hash);527 const empty_hash = P.Hash.hash(&.{});
528 p.version = .{ .tls_1_3 = undefined };528 p.version = .{ .tls_1_3 = undefined };
529 const pv = &p.version.tls_1_3;529 const pv = &p.version.tls_1_3;
530 const hs_derived_secret = hkdfExpandLabel(P.Hkdf, early_secret, "derived", &empty_hash, P.Hash.digest_length);530 const hs_derived_secret = hkdfExpandLabel(P.Hkdf, early_secret, "derived", &empty_hash, P.Hash.digest_length);
src/Package.zig+1-2
...@@ -132,8 +132,7 @@ pub const Hash = struct {...@@ -132,8 +132,7 @@ pub const Hash = struct {
132 @memcpy(result.bytes[i..][0..sub_path.len], sub_path);132 @memcpy(result.bytes[i..][0..sub_path.len], sub_path);
133 return result;133 return result;
134 }134 }
135 var bin_digest: [Algo.digest_length]u8 = undefined;135 const bin_digest = Algo.hash(sub_path);
136 Algo.hash(sub_path, &bin_digest);
137 _ = std.fmt.bufPrint(result.bytes[i..], "{x}", .{&bin_digest}) catch unreachable;136 _ = std.fmt.bufPrint(result.bytes[i..], "{x}", .{&bin_digest}) catch unreachable;
138 return result;137 return result;
139 }138 }
src/link/MachO/CodeSignature.zig+2-2
...@@ -307,7 +307,7 @@ pub fn writeAdhocSignature(...@@ -307,7 +307,7 @@ pub fn writeAdhocSignature(
307 var buf = std.ArrayList(u8).init(allocator);307 var buf = std.ArrayList(u8).init(allocator);
308 defer buf.deinit();308 defer buf.deinit();
309 try req.write(buf.writer());309 try req.write(buf.writer());
310 Sha256.hash(buf.items, &hash);310 hash = Sha256.hash(buf.items);
311 self.code_directory.addSpecialHash(req.slotType(), hash);311 self.code_directory.addSpecialHash(req.slotType(), hash);
312312
313 try blobs.append(.{ .requirements = req });313 try blobs.append(.{ .requirements = req });
...@@ -319,7 +319,7 @@ pub fn writeAdhocSignature(...@@ -319,7 +319,7 @@ pub fn writeAdhocSignature(
319 var buf = std.ArrayList(u8).init(allocator);319 var buf = std.ArrayList(u8).init(allocator);
320 defer buf.deinit();320 defer buf.deinit();
321 try ents.write(buf.writer());321 try ents.write(buf.writer());
322 Sha256.hash(buf.items, &hash);322 hash = Sha256.hash(buf.items);
323 self.code_directory.addSpecialHash(ents.slotType(), hash);323 self.code_directory.addSpecialHash(ents.slotType(), hash);
324324
325 try blobs.append(.{ .entitlements = ents });325 try blobs.append(.{ .entitlements = ents });
src/link/MachO/hasher.zig+1-1
...@@ -58,7 +58,7 @@ pub fn ParallelHasher(comptime Hasher: type) type {...@@ -58,7 +58,7 @@ pub fn ParallelHasher(comptime Hasher: type) type {
58 const tracy = trace(@src());58 const tracy = trace(@src());
59 defer tracy.end();59 defer tracy.end();
60 err.* = file.preadAll(buffer, fstart);60 err.* = file.preadAll(buffer, fstart);
61 Hasher.hash(buffer, out);61 out.* = Hasher.hash(buffer);
62 }62 }
6363
64 const Self = @This();64 const Self = @This();
src/link/MachO/uuid.zig+1-1
...@@ -28,7 +28,7 @@ pub fn calcUuid(comp: *const Compilation, file: fs.File, file_size: u64, out: *[...@@ -28,7 +28,7 @@ pub fn calcUuid(comp: *const Compilation, file: fs.File, file_size: u64, out: *[
28 @memcpy(final_buffer[i * Md5.digest_length ..][0..Md5.digest_length], &hash);28 @memcpy(final_buffer[i * Md5.digest_length ..][0..Md5.digest_length], &hash);
29 }29 }
3030
31 Md5.hash(final_buffer, out);31 out.* = Md5.hash(final_buffer);
32 conform(out);32 conform(out);
33}33}
3434