std.crypto.hash_composition.Composition
类型函数
两个哈希函数的复合 H1 o H2,其 API 与普通哈希函数相同。
级联哈希的安全级别不会超过其中最弱函数的安全级别。
不过,SHA-256 等 Merkle–Damgård 构造易受长度扩展攻击,
在某些条件下,无需知道 x 即可高效计算 H(x||e)。
复合两个哈希函数是抵御此类攻击的常见方式。
SHA-3、BLAKE2、BLAKE3 等现代哈希函数无需如此处理。
pub fn Composition(comptime H1: type, comptime H2: type) type
参数
H1: type
H2: type
字段
H1: H1
H2: H2
std.crypto.hash_composition.Composition.block_length
常量
块长度,单位为字节。
std.crypto.hash_composition.Composition.digest_length
常量
哈希输出的长度,单位为字节。
std.crypto.hash_composition.Composition.final
函数
计算累积内容的最终哈希:H1(H2(b))。
pub fn final(d: *Self, out: *[digest_length]u8) void
参数
d: *Self
out: *[digest_length]u8
std.crypto.hash_composition.Composition.finalResult
函数
pub fn finalResult(d: *Self) [digest_length]u8
参数
d: *Self
std.crypto.hash_composition.Composition.hash
函数
计算 H1(H2(b))。
pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void
参数
std.crypto.hash_composition.Composition.init
函数
使用指定选项初始化哈希组合。
pub fn init(options: Options) Self
参数
options: Options
std.crypto.hash_composition.Composition.Options
容器
两种哈希算法的选项。
字段
H1: H1.Options = .{}H1 的选项。
H2: H2.Options = .{}H2 的选项。
std.crypto.hash_composition.Composition.Self
别名
std.crypto.hash_composition.Composition.update
函数
向哈希添加内容。
pub fn update(d: *Self, b: []const u8) void
参数
d: *Self
b: []const u8
std.crypto.hash_composition.sha2
别名
别名目标 std.crypto.hash.sha2
std.crypto.hash_composition.Sha256oSha256
类型
SHA-256(SHA-256())
std.crypto.hash_composition.Sha384oSha384
类型
SHA-384(SHA-384())
std.crypto.hash_composition.Sha512oSha512
类型
SHA-512(SHA-512())
std.crypto.hash_composition.std
别名
别名目标 std
源码
std/crypto/hash_composition.zig
const std = @import("../std.zig");
const sha2 = std.crypto.hash.sha2;
/// 两个哈希函数的复合 H1 o H2,其 API 与普通哈希函数相同。
///
/// 级联哈希的安全级别不会超过其中最弱函数的安全级别。
///
/// 不过,SHA-256 等 Merkle–Damgård 构造易受长度扩展攻击,
/// 在某些条件下,无需知道 `x` 即可高效计算 `H(x||e)`。
/// 复合两个哈希函数是抵御此类攻击的常见方式。
///
/// SHA-3、BLAKE2、BLAKE3 等现代哈希函数无需如此处理。
pub fn Composition(comptime H1: type, comptime H2: type) type {
return struct {
const Self = @This();
H1: H1,
H2: H2,
/// 哈希输出的长度,单位为字节。
pub const digest_length = H1.digest_length;
/// 块长度,单位为字节。
pub const block_length = H1.block_length;
/// 两种哈希算法的选项。
pub const Options = struct {
/// H1 的选项。
H1: H1.Options = .{},
/// H2 的选项。
H2: H2.Options = .{},
};
/// 使用指定选项初始化哈希组合。
pub fn init(options: Options) Self {
return Self{ .H1 = H1.init(options.H1), .H2 = H2.init(options.H2) };
}
/// 计算 H1(H2(b))。
pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
var d = Self.init(options);
d.update(b);
d.final(out);
}
/// 向哈希添加内容。
pub fn update(d: *Self, b: []const u8) void {
d.H2.update(b);
}
/// 计算累积内容的最终哈希:H1(H2(b))。
pub fn final(d: *Self, out: *[digest_length]u8) void {
var H2_digest: [H2.digest_length]u8 = undefined;
d.H2.final(&H2_digest);
d.H1.update(&H2_digest);
d.H1.final(out);
}
pub fn finalResult(d: *Self) [digest_length]u8 {
var result: [digest_length]u8 = undefined;
d.final(&result);
return result;
}
};
}
/// SHA-256(SHA-256())
pub const Sha256oSha256 = Composition(sha2.Sha256, sha2.Sha256);
/// SHA-384(SHA-384())
pub const Sha384oSha384 = Composition(sha2.Sha384, sha2.Sha384);
/// SHA-512(SHA-512())
pub const Sha512oSha512 = Composition(sha2.Sha512, sha2.Sha512);
test "Hash composition" {
const Sha256 = sha2.Sha256;
const msg = "test";
var out: [Sha256oSha256.digest_length]u8 = undefined;
Sha256oSha256.hash(msg, &out, .{});
var t: [Sha256.digest_length]u8 = undefined;
Sha256.hash(msg, &t, .{});
var out2: [Sha256.digest_length]u8 = undefined;
Sha256.hash(&t, &out2, .{});
try std.testing.expectEqualSlices(u8, &out, &out2);
}