Commit e579c88f4c

Andrew Kelley <andrew@ziglang.org>
2023-01-08 02:06:26
std.crypto.siphash: add finalResult() and peek()
Useful for avoiding mutable state when using this API.
1 parent 87b2234
Changed files (1)
lib
std
lib/std/crypto/siphash.zig
@@ -87,6 +87,11 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
             self.msg_len +%= @truncate(u8, b.len);
         }
 
+        pub fn peek(self: Self) [digest_length]u8 {
+            var copy = self;
+            return copy.finalResult();
+        }
+
         pub fn final(self: *Self, b: []const u8) T {
             std.debug.assert(b.len < 8);
 
@@ -124,6 +129,12 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
             return (@as(u128, b2) << 64) | b1;
         }
 
+        pub fn finalResult(self: *Self) [digest_length]u8 {
+            var result: [digest_length]u8 = undefined;
+            self.final(&result);
+            return result;
+        }
+
         fn round(self: *Self, b: [8]u8) void {
             const m = mem.readIntLittle(u64, b[0..8]);
             self.v3 ^= m;
@@ -205,12 +216,23 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
             self.buf_len += @intCast(u8, b[off + aligned_len ..].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.writeIntLittle(T, out, self.state.final(self.buf[0..self.buf_len]));
         }
 
+        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);