fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type state: Statebuf: [8]u8buf_len: usizepub fn init(key: *const [key_length]u8) SelfInitialize a state for a SipHash function
pub fn update(self: *Self, b: []const u8) voidAdd data to the state
pub fn peek(self: Self) [mac_length]u8pub fn final(self: *Self, out: *[mac_length]u8) voidReturn an authentication tag for the current state Assumes out is less than or equal to mac_length.
pub fn finalResult(self: *Self) [mac_length]u8pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) voidReturn an authentication tag for a message and a key
pub fn finalInt(self: *Self) TReturn an authentication tag for the current state, as an integer
pub fn toInt(msg: []const u8, key: *const [key_length]u8) TReturn an authentication tag for a message and a key, as an integer
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type {
assert(T == u64 or T == u128);
assert(c_rounds > 0 and d_rounds > 0);
return struct {
const State = SipHashStateless(T, c_rounds, d_rounds);
const Self = @This();
pub const key_length = 16;
pub const mac_length = @sizeOf(T);
pub const block_length = 8;
state: State,
buf: [8]u8,
buf_len: usize,
/// Initialize a state for a SipHash function
pub fn init(key: *const [key_length]u8) Self {
return Self{
.state = State.init(key),
.buf = undefined,
.buf_len = 0,
};
}
/// Add data to the state
pub fn update(self: *Self, b: []const u8) void {
var off: usize = 0;
if (self.buf_len != 0 and self.buf_len + b.len >= 8) {
off += 8 - self.buf_len;
@memcpy(self.buf[self.buf_len..][0..off], b[0..off]);
self.state.update(self.buf[0..]);
self.buf_len = 0;
}
const remain_len = b.len - off;
const aligned_len = remain_len - (remain_len % 8);
self.state.update(b[off .. off + aligned_len]);
const b_slice = b[off + aligned_len ..];
@memcpy(self.buf[self.buf_len..][0..b_slice.len], b_slice);
self.buf_len += @as(u8, @intCast(b_slice.len));
}
pub fn peek(self: Self) [mac_length]u8 {
var copy = self;
return copy.finalResult();
}
/// Return an authentication tag for the current state
/// Assumes `out` is less than or equal to `mac_length`.
pub fn final(self: *Self, out: *[mac_length]u8) void {
mem.writeInt(T, out, self.state.final(self.buf[0..self.buf_len]), .little);
}
pub fn finalResult(self: *Self) [mac_length]u8 {
var result: [mac_length]u8 = undefined;
self.final(&result);
return result;
}
/// Return an authentication tag for a message and a key
pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {
var ctx = Self.init(key);
ctx.update(msg);
ctx.final(out);
}
/// Return an authentication tag for the current state, as an integer
pub fn finalInt(self: *Self) T {
return self.state.final(self.buf[0..self.buf_len]);
}
/// Return an authentication tag for a message and a key, as an integer
pub fn toInt(msg: []const u8, key: *const [key_length]u8) T {
return State.hash(msg, key);
}
};
}