A Rust Implementation of China's Standards of Encryption Algorithms(SM2/SM3/SM4)
Add this to your Cargo.toml
:
toml
[dependencies]
gm-rs = "0.3.1"
```rust use crate::sm3::sm3_hash;
fn main() { let hash = sm3hash(b"abc"); let r = hex::encode(hash.asref().unwrap()); assert_eq!("66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", r); }
```
```rust use crate::sm2::key::{gen_keypair, CompressModle};
fn main() { let (pk, sk) = genkeypair(CompressModle::Compressed).unwrap(); let msg = "你好 world,asjdkajhdjadahkubbhj12893718927391873891,@@!! world,1231 wo12321321313asdadadahello world,hello world".asbytes(); let encrypt = pk.encrypt(msg).unwrap(); let plain = sk.decrypt(&encrypt).unwrap(); assert_eq!(msg, plain) }
```
```rust use crate::sm2::signature;
fn main() { let msg = b"hello"; let (pk, sk) = genkeypair(CompressModle::Compressed).unwrap(); let signature = signature::sign(None, msg, &sk.d, &pk).unwrap(); let r = signature.verify(None, msg, &pk).unwrap(); println!("testsign_verify = {}", r) }
```
```rust use crate::sm2::exchange::Exchange;
fn main() { let ida = "alice123@qq.com"; let idb = "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);
}
```