DoxigAlpha

init

Function parameters

Parameters

#
key:[]const u8

Type definitions in this namespace

Types

#

Functions in this namespace

Functions

#

Source

Implementation

#
pub fn init(key: []const u8) Self {
    var ctx: Self = undefined;
    var scratch: [Hash.block_length]u8 = undefined;
    var i_key_pad: [Hash.block_length]u8 = undefined;

    // Normalize key length to block size of hash
    if (key.len > Hash.block_length) {
        Hash.hash(key, scratch[0..mac_length], .{});
        @memset(scratch[mac_length..Hash.block_length], 0);
    } else if (key.len < Hash.block_length) {
        @memcpy(scratch[0..key.len], key);
        @memset(scratch[key.len..Hash.block_length], 0);
    } else {
        @memcpy(&scratch, key);
    }

    for (&ctx.o_key_pad, 0..) |*b, i| {
        b.* = scratch[i] ^ 0x5c;
    }

    for (&i_key_pad, 0..) |*b, i| {
        b.* = scratch[i] ^ 0x36;
    }

    ctx.hash = Hash.init(.{});
    ctx.hash.update(&i_key_pad);
    return ctx;
}