From c84147f90dd99af1352e69f698be48964573ee07 Mon Sep 17 00:00:00 2001 From: Meghan Date: Sat, 20 Nov 2021 01:37:17 -0800 Subject: [PATCH] std: add `writer` methods on all crypto.hash types (#10168) --- lib/std/crypto/blake2.zig | 12 ++++++++++++ lib/std/crypto/blake3.zig | 12 ++++++++++++ lib/std/crypto/gimli.zig | 12 ++++++++++++ lib/std/crypto/sha1.zig | 12 ++++++++++++ lib/std/crypto/sha2.zig | 12 ++++++++++++ lib/std/crypto/sha3.zig | 12 ++++++++++++ lib/std/crypto/siphash.zig | 12 ++++++++++++ 7 files changed, 84 insertions(+) diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index e2107dda56..80557eb255 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -184,6 +184,18 @@ pub fn Blake2s(comptime out_bits: usize) type { r.* ^= v[i] ^ v[i + 8]; } } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Self, Error, write); + + fn write(self: *Self, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Self) Writer { + return .{ .context = self }; + } }; } diff --git a/lib/std/crypto/blake3.zig b/lib/std/crypto/blake3.zig index b2077bb9e7..7f2c21be33 100644 --- a/lib/std/crypto/blake3.zig +++ b/lib/std/crypto/blake3.zig @@ -472,6 +472,18 @@ pub const Blake3 = struct { } output.rootOutputBytes(out_slice); } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Blake3, Error, write); + + fn write(self: *Blake3, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Blake3) Writer { + return .{ .context = self }; + } }; // Use named type declarations to workaround crash with anonymous structs (issue #4373). diff --git a/lib/std/crypto/gimli.zig b/lib/std/crypto/gimli.zig index fc4b5313b8..3e2fad4229 100644 --- a/lib/std/crypto/gimli.zig +++ b/lib/std/crypto/gimli.zig @@ -257,6 +257,18 @@ pub const Hash = struct { self.state.squeeze(out); } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Self, Error, write); + + fn write(self: *Self, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Self) Writer { + return .{ .context = self }; + } }; pub fn hash(out: []u8, in: []const u8, options: Hash.Options) void { diff --git a/lib/std/crypto/sha1.zig b/lib/std/crypto/sha1.zig index b464fb03f5..99289e35c4 100644 --- a/lib/std/crypto/sha1.zig +++ b/lib/std/crypto/sha1.zig @@ -256,6 +256,18 @@ pub const Sha1 = struct { d.s[3] +%= v[3]; d.s[4] +%= v[4]; } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Self, Error, write); + + fn write(self: *Self, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Self) Writer { + return .{ .context = self }; + } }; const htest = @import("test.zig"); diff --git a/lib/std/crypto/sha2.zig b/lib/std/crypto/sha2.zig index 2d2a70c87f..b7a78c4b44 100644 --- a/lib/std/crypto/sha2.zig +++ b/lib/std/crypto/sha2.zig @@ -277,6 +277,18 @@ fn Sha2x32(comptime params: Sha2Params32) type { d.s[6] +%= v[6]; d.s[7] +%= v[7]; } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Self, Error, write); + + fn write(self: *Self, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Self) Writer { + return .{ .context = self }; + } }; } diff --git a/lib/std/crypto/sha3.zig b/lib/std/crypto/sha3.zig index db8126d183..ccc0775df1 100644 --- a/lib/std/crypto/sha3.zig +++ b/lib/std/crypto/sha3.zig @@ -78,6 +78,18 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { mem.copy(u8, out[op..], d.s[0..len]); } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Self, Error, write); + + fn write(self: *Self, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Self) Writer { + return .{ .context = self }; + } }; } diff --git a/lib/std/crypto/siphash.zig b/lib/std/crypto/siphash.zig index c984e04490..50706a0f35 100644 --- a/lib/std/crypto/siphash.zig +++ b/lib/std/crypto/siphash.zig @@ -231,6 +231,18 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) pub fn toInt(msg: []const u8, key: *const [key_length]u8) T { return State.hash(msg, key); } + + pub const Error = error{}; + pub const Writer = std.io.Writer(*Self, Error, write); + + fn write(self: *Self, bytes: []const u8) Error!usize { + self.update(bytes); + return bytes.len; + } + + pub fn writer(self: *Self) Writer { + return .{ .context = self }; + } }; }