Crates.io | gmsm |
lib.rs | gmsm |
version | 0.1.0 |
source | src |
created_at | 2021-08-16 05:56:46.829534 |
updated_at | 2021-08-27 07:28:11.317157 |
description | A Rust Library of China's Standards of Encryption Algorithms (SM2/3/4) |
homepage | |
repository | https://github.com/nfjBill/rust-gmsm |
max_upload_size | |
id | 437667 |
size | 111,760 |
gmsm is an open source pure rust library of China Cryptographic Algorithm Standards.
Add this to your Cargo.toml
:
[dependencies]
gmsm = "0.1"
use gmsm::sm2::*;
fn main() {
let keypair = sm2_generate_key_hex();
let pri_key = keypair.pri_hex;
// 78b39862c5c98997bba5c5f8c62db26566329bac2f3d957ddac49f8d890d258a
let pub_hex = keypair.pub_hex;
// 04861442c01bd02476b1f30ecff95c8604d4388acc154537c74c437cba59807fb507175fa9b35a9e14c1dbd5c018355300ef593675189eb74af89b0b1030ecef34
let plain_str = "hello world, this is sm2 test!";
let cipher = sm2_encrypt_c1c3c2(plain_str, &pub_hex);
// 0468b34613d746beec82d8db74b401073aa044fbe0e6c0e74d30efa0cf5f7d30ebc2ac6c64e609dc11708cb612d01403f2e5a8b773199191ba7230a47165d69b058ec6a38681aafdb1b4ced7656eba2e3e12e941e3b0fb1ef1d00e15c43ce9a8f5f920a8da4d49b81405e308ef63dc25ffd039d8f1eeafd56de9387f0219a0
let plain = sm2_decrypt_c1c3c2(&cipher, &pri_key);
// hello world, this is sm2 test!
assert_eq!(plain, plain_str);
}
use gmsm::sm3::sm3_hex;
fn main() {
let s = sm3_hex("abc");
println!("{}", s)
// "66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0";
}
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);
// 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.