Type Function Keccak [src]
A generic Keccak hash function.
Prototype
pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim: u8, comptime rounds: u5) type
Parameters
f: u11
output_bits: u11
default_delim: u8
rounds: u5
Source
pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim: u8, comptime rounds: u5) type {
comptime assert(output_bits > 0 and output_bits * 2 < f and output_bits % 8 == 0); // invalid output length
const State = KeccakState(f, output_bits * 2, rounds);
return struct {
const Self = @This();
st: State,
/// The output length, in bytes.
pub const digest_length = std.math.divCeil(comptime_int, output_bits, 8) catch unreachable;
/// The block length, or rate, in bytes.
pub const block_length = State.rate;
/// The delimiter can be overwritten in the options.
pub const Options = struct { delim: u8 = default_delim };
/// Initialize a Keccak hash function.
pub fn init(options: Options) Self {
return Self{ .st = .{ .delim = options.delim } };
}
/// Hash a slice of bytes.
pub fn hash(bytes: []const u8, out: *[digest_length]u8, options: Options) void {
var st = Self.init(options);
st.update(bytes);
st.final(out);
}
/// Absorb a slice of bytes into the state.
pub fn update(self: *Self, bytes: []const u8) void {
self.st.absorb(bytes);
}
/// Return the hash of the absorbed bytes.
pub fn final(self: *Self, out: *[digest_length]u8) void {
self.st.pad();
self.st.squeeze(out[0..]);
}
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 };
}
};
}