6个版本 (3个重大更新)

0.13.0 2024年4月11日
0.12.2 2024年3月21日
0.12.1 2023年12月15日
0.11.0 2023年12月11日
0.10.0 2023年4月17日

#1513 in 加密学

Download history 7/week @ 2024-05-20 3/week @ 2024-05-27 13/week @ 2024-06-03 23/week @ 2024-06-10 9/week @ 2024-06-17 39/week @ 2024-07-01 10/week @ 2024-07-08 37/week @ 2024-07-22 24/week @ 2024-07-29 24/week @ 2024-08-05

每月85次下载

MIT 许可证

1.5MB
19K SLoC

gm-sm2

中国加密算法标准SM2的纯Rust高性能实现

示例

加密和解密

 use gm_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 gm_sm2::key::{gen_keypair, CompressModle};
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 gm_sm2::key::{CompressModle};
fn main() {
    let msg = b"hello";
    let pk_hex = hex::decode("04D5548C7825CBB56150A3506CD57464AF8A1AE0519DFAF3C58221DC810CAF28DD921073768FE3D59CE54E79A49445CF73FED23086537027264D168946D479533E").unwrap();
    let pk = gm_sm2::key::Sm2PublicKey::new(&pk_hex[..], CompressModle::Uncompressed).unwrap();
    let sk_hex =
        hex::decode("128b2fa8bd433c6c068c8d803dff79792a519a55171b1b650c23661d15897263").unwrap();
    let sk = gm_sm2::key::Sm2PrivateKey::new(&sk_hex[..], CompressModle::Compressed).unwrap();

    let signature = sk.sign(None, msg).unwrap();
    pk.verify(None, msg, &signature).unwrap();
}

密钥交换

use gm_sm2::exchange::Exchange;
use gm_sm2::key::{gen_keypair, CompressModle};

fn main() {
    let id_a = "[email protected]";
    let id_b = "[email protected]";

    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);
}

参考

libsm

依赖项

~2.5MB
~45K SLoC