gmsm is an open source pure rust library of China Cryptographic Algorithm Standards.
Add this to your Cargo.toml
:
toml
[dependencies]
gmsm = "0.1"
```rust use gmsm::sm2::*;
fn main() { let keypair = sm2generatekeyhex(); let prikey = keypair.prihex; // 78b39862c5c98997bba5c5f8c62db26566329bac2f3d957ddac49f8d890d258a let pubhex = keypair.pubhex; // 04861442c01bd02476b1f30ecff95c8604d4388acc154537c74c437cba59807fb507175fa9b35a9e14c1dbd5c018355300ef593675189eb74af89b0b1030ecef34 let plainstr = "hello world, this is sm2 test!"; let cipher = sm2encryptc1c3c2(plainstr, &pubhex); // 0468b34613d746beec82d8db74b401073aa044fbe0e6c0e74d30efa0cf5f7d30ebc2ac6c64e609dc11708cb612d01403f2e5a8b773199191ba7230a47165d69b058ec6a38681aafdb1b4ced7656eba2e3e12e941e3b0fb1ef1d00e15c43ce9a8f5f920a8da4d49b81405e308ef63dc25ffd039d8f1eeafd56de9387f0219a0 let plain = sm2decryptc1c3c2(&cipher, &pri_key); // hello world, this is sm2 test!
assert_eq!(plain, plain_str);
} ```
```rust use gmsm::sm3::sm3_hex;
fn main() { let s = sm3_hex("abc");
println!("{}", s)
// "66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0";
} ```
```rust use gmsm::sm4::{sm4ecbencrypthex, sm4ecbdecrypthex, sm4cbcencrypthex, sm4cbcdecrypthex};
fn main() { let key = "8A3F8665AAEE6F7A0CB8F40B971E3373"; let iv = "88BA27B390F466ABE7C4327E1E60270B"; let plain_str = "hello world, this is sm4 test!";
let ecb_cipher = sm4_ecb_encrypt_hex(plain_str, key);
println!("{}", s);
// 9AA0BCBF487682AEAF7C640230568083452F4EDE1B8E265CC07A2F8CE07FC2E7
let cbc_cipher = sm4_cbc_encrypt_hex(plain_str, key, iv);
println!("{}", s);
// 92662AD8A11D165EEF617AE3EDC4F9D4012A4C3CE7F42B15F26D1DA404CD97E0
// let ecb_plain = sm4_ecb_decrypt_hex(cipher_str, key);
// let cbc_plain = sm4_cbc_decrypt_hex(cipher_str, key, iv);
} ```
gmsm is currently under the Apache 2.0 license.