Sign in

    smcrypto

    国密算法库: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

    sm3
    sm4
    chinese-national-cryptography
    hash
    cipher
    hmac
    kdf
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    14 hours ago
    Downloads
    3

    #wbgxiaosu/smcrypto

    国密算法库(SM3 + SM4),纯 MoonBit 实现、零依赖。

    #安装

    moon add wbgxiaosu/smcrypto

    #SM3 密码杂凑(GB/T 32905-2016)

    // 一次性计算(返回十六进制串)
    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 字节

    #SM4 分组密码(GB/T 32907-2016)

    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)

    另有 ECB 模式(sm4_encrypt_ecb / sm4_decrypt_ecb,PKCS7 填充)、CTR 计数器模式(sm4_encrypt_ctr / sm4_decrypt_ctr,无填充输出等长)与单块操作(Sm4::new + encrypt_block / decrypt_block)。

    #HMAC-SM3 与 SM3-KDF

    // 消息认证码(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)

    #验证

    • 国标标准测试向量全通过(SM3 示例 1/2、SM4 标准示例)
    • 与 Python gmssl 交叉对拍:51 条 SM3 向量、24 组 SM4 向量
    • wasm-gc / js / native 三后端测试一致

    moon check # 零警告 moon test # 25 组测试 moon run cmd/main # 可复现演示

    #License

    Apache-2.0

    sm3_digest

    fn sm3_digest(data : Bytes) -> Bytes

    计算 SM3 摘要,返回 32 字节。

    sm3_hex_digest

    fn sm3_hex_digest(data : Bytes) -> String

    计算 SM3 摘要,返回小写十六进制字符串。

    sm3_hmac

    fn sm3_hmac(key : Bytes, message : Bytes) -> Bytes

    计算 HMAC-SM3 消息认证码(RFC 2104 结构)。

    sm3_kdf

    fn sm3_kdf(z : Bytes, klen : Int) -> Bytes

    SM3 密钥派生函数 KDF(GB/T 32918.3-2016 5.4.2)。

    sm4_decrypt_cbc

    fn sm4_decrypt_cbc(key : Bytes, iv : Bytes, ciphertext : Bytes) -> Result[Bytes,
    Sm4Error
    ]

    SM4 CBC 模式解密(去 PKCS7 填充)。

    sm4_decrypt_ctr

    fn sm4_decrypt_ctr(key : Bytes, counter : Bytes, ciphertext : Bytes) -> Result[Bytes,
    Sm4Error
    ]

    SM4 CTR 模式解密(与加密同一变换)。

    sm4_decrypt_ecb

    fn sm4_decrypt_ecb(key : Bytes, ciphertext : Bytes) -> Result[Bytes,
    Sm4Error
    ]

    SM4 ECB 模式解密(去 PKCS7 填充)。

    sm4_encrypt_cbc

    fn sm4_encrypt_cbc(key : Bytes, iv : Bytes, plaintext : Bytes) -> Result[Bytes,
    Sm4Error
    ]

    SM4 CBC 模式加密(含 PKCS7 填充)。

    sm4_encrypt_ctr

    fn sm4_encrypt_ctr(key : Bytes, counter : Bytes, plaintext : Bytes) -> Result[Bytes,
    Sm4Error
    ]

    SM4 CTR 模式加密(无填充,输出与明文等长)。

    sm4_encrypt_ecb

    fn sm4_encrypt_ecb(key : Bytes, plaintext : Bytes) -> Result[Bytes,
    Sm4Error
    ]

    SM4 ECB 模式加密(含 PKCS7 填充)。

    sm4_new

    从 16 字节密钥创建 SM4 密码器。

    Source Files