Skip to main content

encryption/hash/
belt.rs

1// Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz
2
3//! Belt-Hash(STB 34.101.31,白俄罗斯国家标准);输出 32 字节。
4
5use belt_hash::BeltHash;
6
7use crate::hash::{NamedDigest, hasher};
8
9hasher!(
10    BeltHashHasher,
11    "belt-hash",
12    "Belt-Hash 哈希器(无状态,可复用)。"
13);
14
15impl NamedDigest for BeltHashHasher {
16    type Core = BeltHash;
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22    use crate::contract::Hasher;
23
24    /// STB 34.101.31-2020 附录 A.11 表 A.23 测试 A.23-1:标准测试数据 H 的
25    /// 前 13 字节(X = B194BAC80A08F53B366D008E58)。
26    const X: [u8; 13] = [
27        0xb1, 0x94, 0xba, 0xc8, 0x0a, 0x08, 0xf5, 0x3b, 0x36, 0x6d, 0x00, 0x8e, 0x58,
28    ];
29
30    #[test]
31    fn matches_stb_appendix_vector() {
32        let hasher = BeltHashHasher::new();
33        assert_eq!(
34            hasher.digest_hex(&X).unwrap(),
35            "abef9725d4c5a83597a367d14494cc2542f20f659ddfecc961a3ec550cba8c75"
36        );
37        assert_eq!(hasher.digest(&X).unwrap().len(), 32);
38    }
39
40    /// digest_hex 输出为 2×摘要长度的小写十六进制。
41    #[test]
42    fn digest_hex_is_lowercase_hex() {
43        let hex = BeltHashHasher::new().digest_hex(&X).unwrap();
44        assert_eq!(hex.len(), 64);
45        assert!(
46            hex.bytes()
47                .all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase())
48        );
49    }
50
51    /// 弱检查:不同输入给出不同摘要。
52    #[test]
53    fn distinct_inputs_distinct_digests() {
54        let hasher = BeltHashHasher::new();
55        assert_ne!(hasher.digest(&X).unwrap(), hasher.digest(b"abc").unwrap());
56    }
57}