std: add writer methods on all crypto.hash types (#10168)

This commit is contained in:
Meghan 2021-11-20 01:37:17 -08:00 committed by GitHub
parent 54bcd1cd51
commit c84147f90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 84 additions and 0 deletions

View File

@ -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 };
}
};
}

View File

@ -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).

View File

@ -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 {

View File

@ -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");

View File

@ -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 };
}
};
}

View File

@ -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 };
}
};
}

View File

@ -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 };
}
};
}