国密算法库:SM3 密码杂凑(GB/T 32905-2016)与 SM4 分组密码(GB/T 32907-2016),纯 MoonBit 实现、零依赖、国标向量对拍 / Pure-MoonBit Chinese national cryptography: SM3 hash and SM4 block cipher with standard test vectors
moon add wbgxiaosu/smcrypto// 一次性计算(返回十六进制串)
let hex : String = @smcrypto.sm3_hex_digest(b"abc")
// = "66c7f0f4..."(国标示例 1)
// 流式计算
let h = @sm3.Sm3::new()
h.update(part1)
h.update(part2)
let digest : Bytes = h.finalize() // 32 字节let key = @hexutil.decode("0123456789abcdeffedcba9876543210").unwrap()
let iv = @hexutil.decode("000102030405060708090a0b0c0d0e0f").unwrap()
let pt = @utf8.encode("攻击开始于黎明!")
let ct = @smcrypto.sm4_encrypt_cbc(key, iv, pt).unwrap()
let back = @smcrypto.sm4_decrypt_cbc(key, iv, ct).unwrap()
assert_eq(back, pt)// 消息认证码(RFC 2104 结构)
let mac : Bytes = @smcrypto.sm3_hmac(key, message)
// 密钥派生(GB/T 32918.3-2016 5.4.2,SM2/SM9 使用)
let derived : Bytes = @smcrypto.sm3_kdf(shared_secret, 32)moon check # 零警告
moon test # 25 组测试
moon run cmd/main # 可复现演示Install
Download zip国密算法库:SM3 密码杂凑(GB/T 32905-2016)与 SM4 分组密码(GB/T 32907-2016),纯 MoonBit 实现、零依赖、国标向量对拍 / Pure-MoonBit Chinese national cryptography: SM3 hash and SM4 block cipher with standard test vectors