Crates.io | gm-rs |
lib.rs | gm-rs |
version | 0.8.0 |
source | src |
created_at | 2022-10-12 04:32:14.618686 |
updated_at | 2023-04-17 01:01:28.386706 |
description | A Rust Implementation of China's Standards of Encryption Algorithms(SM2/SM3/SM4) |
homepage | https://github.com/CrayfishGo/gm-rs.git |
repository | |
max_upload_size | |
id | 685885 |
size | 305,822 |
A Pure Rust High-Performance Implementation of China's Standards of Encryption Algorithms(SM2/SM3/SM4)
Add this to your Cargo.toml
:
[dependencies]
gm-rs = "0.7.0"
use crate::sm4::Sm4Cipher;
use hex_literal::hex;
fn main() {
let key = hex!("0123456789abcdeffedcba9876543210");
let plaintext = key.clone();
let ciphertext = hex!("681edf34d206965e86b3e94f536e4246");
let cipher = Sm4Cipher::new(&key).unwrap();
let enc = cipher.encrypt(&plaintext).unwrap();
assert_eq!(&ciphertext, enc.as_slice());
}
use crate::sm3::sm3_hash;
fn main() {
let hash = sm3_hash(b"abc");
let r = hex::encode(hash.as_ref().unwrap());
assert_eq!("66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", r);
}
use crate::sm2::key::{gen_keypair, CompressModle};
fn main() {
let (pk, sk) = gen_keypair(CompressModle::Compressed).unwrap();
let msg = "你好 world,asjdkajhdjadahkubbhj12893718927391873891,@@!! world,1231 wo12321321313asdadadahello world,hello world".as_bytes();
let encrypt = pk.encrypt(msg).unwrap();
let plain = sk.decrypt(&encrypt).unwrap();
assert_eq!(msg, plain)
}
use crate::sm2::signature;
fn main() {
let msg = b"hello";
let (pk, sk) = gen_keypair(CompressModle::Compressed).unwrap();
let signature = sk.sign(None, msg).unwrap();
pk.verify(None, msg, &signature).unwrap()
}
use crate::sm2::exchange::Exchange;
fn main() {
let id_a = "alice123@qq.com";
let id_b = "bob456@qq.com";
let (pk_a, sk_a) = gen_keypair(CompressModle::Compressed).unwrap();
let (pk_b, sk_b) = gen_keypair(CompressModle::Compressed).unwrap();
let mut user_a = Exchange::new(8, Some(id_a), &pk_a, &sk_a, Some(id_b), &pk_b).unwrap();
let mut user_b = Exchange::new(8, Some(id_b), &pk_b, &sk_b, Some(id_a), &pk_a).unwrap();
let ra_point = user_a.exchange_1().unwrap();
let (rb_point, sb) = user_b.exchange_2(&ra_point).unwrap();
let sa = user_a.exchange_3(&rb_point, sb).unwrap();
let succ = user_b.exchange_4(sa, &ra_point).unwrap();
println!("test_key_exchange = {}", succ);
assert_eq!(user_a.k, user_b.k);
}