Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › hash › verify Public Namespace verify Doc Source Public Namespace Values Functions fn hashMaybeSeed (comptime hash_fn : anytype , seed : anytype , buf : []const u8 ) @typeInfo (@TypeOf (hash_fn )).@"fn" .return_type .? fn initMaybeSeed (comptime Hash : anytype , seed : anytype ) Hash pub fn smhasher (comptime hash_fn : anytype ) u32 pub fn iterativeApi (comptime Hash : anytype ) !void File hash/verify.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 const std = @import ("std " );
fn hashMaybeSeed (comptime hash_fn : anytype , seed : anytype , buf : []const u8 ) @typeInfo (@TypeOf (hash_fn )).@"fn" .return_type .? {
const HashFn = @typeInfo (@TypeOf (hash_fn )).@"fn" ;
if (HashFn .param_types .len > 1 ) {
if (@typeInfo (HashFn .param_types [0 ].?) == .int ) {
return hash_fn (@intCast (seed ), buf );
} else {
return hash_fn (buf , @intCast (seed ));
}
} else {
return hash_fn (buf );
}
}
fn initMaybeSeed (comptime Hash : anytype , seed : anytype ) Hash {
const HashFn = @typeInfo (@TypeOf (Hash .init )).@"fn" ;
if (HashFn .param_types .len == 1 ) {
return Hash .init (@intCast (seed ));
} else {
return Hash .init ();
}
}
// Returns a verification code, the same as used by SMHasher.
//
// Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255, using 256-N as seed.
// First four-bytes of the hash, interpreted as little-endian is the verification code.
pub fn smhasher (comptime hash_fn : anytype ) u32 {
const HashFnTy = @typeInfo (@TypeOf (hash_fn )).@"fn" ;
const HashResult = HashFnTy .return_type .?;
const hash_size = @sizeOf (HashResult );
var buf : [256 ]u8 = undefined ;
var buf_all : [256 * hash_size ]u8 = undefined ;
for (0 ..256 ) |i | {
buf [i ] = @intCast (i );
const h = hashMaybeSeed (hash_fn , 256 - i , buf [0 ..i ]);
std .mem .writeInt (HashResult , buf_all [i * hash_size ..][0 ..hash_size ], h , .little );
}
return @truncate (hashMaybeSeed (hash_fn , 0 , buf_all [0 ..]));
}
pub fn iterativeApi (comptime Hash : anytype ) !void {
// Sum(1..32) = 528
var buf : [528 ]u8 = @splat (0 );
var len : usize = 0 ;
const seed = 0 ;
var hasher = initMaybeSeed (Hash , seed );
for (1 ..32 ) |i | {
const r = hashMaybeSeed (Hash .hash , seed , buf [0 .. len + i ]);
hasher .update (buf [len ..][0 ..i ]);
const f1 = hasher .final ();
const f2 = hasher .final ();
if (f1 != f2 ) return error .IterativeHashWasNotIdempotent ;
if (f1 != r ) return error .IterativeHashDidNotMatchDirect ;
len += i ;
}
}