gmsm
gmsm is an open source pure rust library of China Cryptographic Algorithm Standards.
GM/T Algorithms
- SM2 (GM/T 0003-2012): elliptic curve cryptographic schemes including digital signature scheme, public key encryption, (authenticated) key exchange protocol and one recommended 256-bit prime field curve sm2p256v1.
- SM3 (GM/T 0004-2012): cryptographic hash function with 256-bit digest length.
- SM4 (GM/T 0002-2012): block cipher with 128-bit key length and 128-bit block size, also named SMS4.
Usage
Add this to your Cargo.toml:
[dependencies]
gmsm = "0.1"
Documents
use gmsm::sm2::*;
fn main() {
let keypair = sm2_generate_key_hex();
let pri_key = keypair.pri_hex;
let pub_hex = keypair.pub_hex;
let plain_str = "hello world, this is sm2 test!";
let cipher = sm2_encrypt_c1c3c2(plain_str, &pub_hex);
let plain = sm2_decrypt_c1c3c2(&cipher, &pri_key);
assert_eq!(plain, plain_str);
}
use gmsm::sm3::sm3_hex;
fn main() {
let s = sm3_hex("abc");
println!("{}", s)
}
use gmsm::sm4::{sm4_ecb_encrypt_hex, sm4_ecb_decrypt_hex, sm4_cbc_encrypt_hex, sm4_cbc_decrypt_hex};
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);
let cbc_cipher = sm4_cbc_encrypt_hex(plain_str, key, iv);
println!("{}", s);
}
License
gmsm is currently under the Apache 2.0 license.