feature. See also
. The project being documented here (as the example) is the Zig library itself.
sha3.KMacLike
fn KMacLike(comptime security_level: u11, comptime default_delim: u8, comptime rounds: u5) type
File
Code
fn KMacLike(comptime security_level: u11, comptime default_delim: u8, comptime rounds: u5) type {
const CShaker = CShakeLike(security_level, default_delim, rounds, "KMAC");
return struct {
const Self = @This();
pub const mac_length = CShaker.digest_length;
pub const mac_length_min = 4;
pub const key_length = security_level / 8;
pub const key_length_min = 0;
pub const block_length = CShaker.block_length;
cshaker: CShaker,
xof_mode: bool = false,
pub const Options = struct {
context: ?[]const u8 = null,
};
pub fn initWithOptions(key: []const u8, options: Options) Self {
var cshaker = CShaker.init(.{ .context = options.context });
const encoded_rate_len = NistLengthEncoding.encode(.left, block_length / 8);
cshaker.update(encoded_rate_len.slice());
const encoded_key_len = NistLengthEncoding.encode(.left, key.len);
cshaker.update(encoded_key_len.slice());
cshaker.update(key);
cshaker.fillBlock();
return Self{
.cshaker = cshaker,
};
}
pub fn init(key: []const u8) Self {
return initWithOptions(key, .{});
}
pub fn update(self: *Self, b: []const u8) void {
self.cshaker.update(b);
}
pub fn final(self: *Self, out: []u8) void {
const encoded_out_len = NistLengthEncoding.encode(.right, out.len);
self.update(encoded_out_len.slice());
self.cshaker.final(out);
}
pub fn squeeze(self: *Self, out: []u8) void {
if (!self.xof_mode) {
const encoded_out_len = comptime NistLengthEncoding.encode(.right, 0);
self.update(encoded_out_len.slice());
self.xof_mode = true;
}
self.cshaker.squeeze(out);
}
pub fn createWithOptions(out: []u8, msg: []const u8, key: []const u8, options: Options) void {
var ctx = Self.initWithOptions(key, options);
ctx.update(msg);
ctx.final(out);
}
pub fn create(out: []u8, msg: []const u8, key: []const u8) void {
var ctx = Self.init(key);
ctx.update(msg);
ctx.final(out);
}
};
}