@@ -36,54 +36,33 @@ pub const Hash = extern struct {
3636
3737 pub const SIZE = 32 ;
3838
39- pub const ZEROES : Hash = .{ .data = .{0 } ** SIZE };
40-
41- /// Hashes the input byte slice(s) using SHA 256.
42- ///
43- /// If the passed-in type contains multiple byte slices, it will
44- /// iterate/recurse over them in order, updating the hasher for all of them
45- /// before finalizing at the end.
46- pub fn generateSha256 (
47- /// May be a slice or array of bytes, or a slice, array, or tuple
48- /// containing slices or arrays of bytes nested with arbitrary depth.
49- ///
50- /// for example:
51- /// - []const u8
52- /// - []const []const u8
53- /// - [2]u8
54- /// - *[13]u8
55- /// - struct { [128]u8, []const []const u8, struct { []const u8 }, ... }
56- data : anytype ,
57- ) Hash {
58- var hasher = Sha256 .init (.{});
59- update (& hasher , data );
60- return .{ .data = hasher .finalResult () };
39+ pub const ZEROES : Hash = .{ .data = @splat (0 ) };
40+
41+ /// Creates a `Hash` by applying SHA256 to the input `data` and using
42+ /// the result of that as the output.
43+ pub fn init (data : []const u8 ) Hash {
44+ var out : [32 ]u8 = undefined ;
45+ Sha256 .hash (data , & out , .{});
46+ return .{ .data = out };
6147 }
6248
63- /// re-hashes the current hash with the mixed-in byte slice(s).
64- pub fn extendAndHash (self : Hash , data : anytype ) Hash {
65- return generateSha256 (.{ self .data , data });
49+ /// Does the same thing as `init`, but updates the hash with each
50+ /// input slice from the `data` list.
51+ pub fn initMany (data : []const []const u8 ) Hash {
52+ var new = Sha256 .init (.{});
53+ for (data ) | d | new .update (d );
54+ return .{ .data = new .finalResult () };
6655 }
6756
68- fn update (hasher : * Sha256 , data : anytype ) void {
69- const T = @TypeOf (data );
70-
71- if (T == Hash or T == * const Hash or T == * Hash ) {
72- hasher .update (& data .data );
73- } else if (@typeInfo (T ) == .@"struct" ) {
74- inline for (data ) | val | update (hasher , val );
75- } else if (std .meta .Elem (T ) == u8 ) switch (@typeInfo (T )) {
76- .array = > hasher .update (& data ),
77- else = > hasher .update (data ),
78- } else {
79- for (data ) | val | update (hasher , val );
80- }
57+ /// re-hashes the current hash with the mixed-in byte slice(s).
58+ pub fn extend (self : Hash , data : []const u8 ) Hash {
59+ return .initMany (&.{ & self .data , data });
8160 }
8261
8362 pub fn eql (self : Hash , other : Hash ) bool {
84- const xx : @Vector (SIZE , u8 ) = self .data ;
85- const yy : @Vector (SIZE , u8 ) = other .data ;
86- return @reduce (.And , xx == yy );
63+ const x : @Vector (SIZE , u8 ) = self .data ;
64+ const y : @Vector (SIZE , u8 ) = other .data ;
65+ return @reduce (.And , x == y );
8766 }
8867
8968 pub fn order (self : * const Hash , other : * const Hash ) std.math.Order {
0 commit comments