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 library itself.
conststd = @import("../std.zig");
constsha2 = std.crypto.hash.sha2;
/// The composition of two hash functions: H1 o H2, with the same API as regular hash functions.////// The security level of a hash cascade doesn't exceed the security level of the weakest function.////// However, Merkle–Damgård constructions such as SHA-256 are vulnerable to length-extension attacks,/// where under some conditions, `H(x||e)` can be efficiently computed without knowing `x`./// The composition of two hash functions is a common defense against such attacks.////// This is not necessary with modern hash functions, such as SHA-3, BLAKE2 and BLAKE3.pubfnComposition(comptimeH1: type, comptimeH2: type) type {
returnstruct {
constSelf = @This();
H1: H1,
H2: H2,
/// The length of the hash output, in bytes.pubconstdigest_length = H1.digest_length;
/// The block length, in bytes.pubconstblock_length = H1.block_length;
/// Options for both hashes.pubconstOptions = struct {
/// Options for H1.H1: H1.Options = .{},
/// Options for H2.H2: H2.Options = .{},
};
/// Initialize the hash composition with the given options.pubfninit(options: Options) Self {
returnSelf{ .H1 = H1.init(options.H1), .H2 = H2.init(options.H2) };
}
/// Compute H1(H2(b)).pubfnhash(b: []constu8, out: *[digest_length]u8, options: Options) void {
vard = Self.init(options);
d.update(b);
d.final(out);
}
/// Add content to the hash.pubfnupdate(d: *Self, b: []constu8) void {
d.H2.update(b);
}
/// Compute the final hash for the accumulated content: H1(H2(b)).pubfnfinal(d: *Self, out: *[digest_length]u8) void {
varH2_digest: [H2.digest_length]u8 = undefined;
d.H2.final(&H2_digest);
d.H1.update(&H2_digest);
d.H1.final(out);
}
};
}
/// SHA-256(SHA-256())pubconstSha256oSha256 = Composition(sha2.Sha256, sha2.Sha256);
/// SHA-384(SHA-384())pubconstSha384oSha384 = Composition(sha2.Sha384, sha2.Sha384);
/// SHA-512(SHA-512())pubconstSha512oSha512 = Composition(sha2.Sha512, sha2.Sha512);
test"Hash composition" {
constSha256 = sha2.Sha256;
constmsg = "test";
varout: [Sha256oSha256.digest_length]u8 = undefined;
Sha256oSha256.hash(msg, &out, .{});
vart: [Sha256.digest_length]u8 = undefined;
Sha256.hash(msg, &t, .{});
varout2: [Sha256.digest_length]u8 = undefined;
Sha256.hash(&t, &out2, .{});
trystd.testing.expectEqualSlices(u8, &out, &out2);
}