1 unstable release
0.1.0 | Dec 6, 2024 |
---|
#529 in Cryptography
137 downloads per month
15KB
203 lines
浦发银行API国密算法
主要依赖库:
smcrypto
库生成密钥对、报文加解密
libsm
库进行改造自定义签名
efficient-sm2
库进行验签
密钥对生成
use spdb_sm2;
fn generate_keypair () {
// 密钥对
// sk: 06f205b3001f2da937238fc3c08312b2dae1fc75f31d57863e86b35bcaf3bfcd
// pk: 0499046e685a996b83f2a64d945d09e5505f1d3fd081991426e46dc158d7c397c64e9a0c3ddb6f1b788507b984a84391e192e4dfb2577ab36e8c67da718e33c097
let (sk, pk) = spdb_sm2::generate_keypair();
}
签名与验签
use spdb_sm2;
fn sign_and_verify () {
let msg = r#"hello world"#;
let pk = "04ff055e4349345eba0fc69362f483f4f408d876dda2520e8e424e81978129da56b19587538253a2406d035a8d9981efeeac60ec72b3308b9a07a5398b61d3d189";
let sk = "6d7964184b735645ef49b3c1ee5a2c2efdbd15d6c9d851c57eef341ed0e1eb1b";
let secret = "ZTPkMS00ZNTP5NzPwNjAu";
// 全报文签名
// MzA0NjAyMjEwMDgwMGM3NDg0NTlkZDQ2MTdlMzMzNWM3OGRjNDJlOGFjZWU0OTg5YmYwYjk2NzFmYWYzZjkxN2ZkNmU0NGFhOTkwMjIxMDBjOGM2YjhiZjI5NzRmNzljYWE3Mjc4MzZjZjgwMTc2MzI0YmI1YjkxZDFkYWQzNjIzMWQyODA2MDVhZTNhNDYy
let signature = spdb_sm2::sign(msg.as_bytes(), pk, sk).unwrap();
// 验签签名
// true
let verify = spdb_sm2::verify(msg.as_bytes(), pk, &signature).unwrap();
// 普通签名
// ZTVlOGU0YzZmYjk0N2JiZDQxNDdmZjgyNTgwYTVhMzgxMjVmN2U5M2Q1MzA0NTg2YmJkNjljMmJiYWZlNWMyZWZlY2JiOWI0YmQzNWQ1YWE3OTZlYTkzY2Q0M2RmNmM2ZGEyMzA1NGJiOTEzMTJmMDE5YzI2YzVjOTZhYWVmYmNmMzkwYjMzZTNlY2Q3MzQzMjMwNWM1YzYzNTQ3ZmI0OQ==
let signature = spdb_sm2::sign_body(msg, secret).unwrap();
}
加密与解密
use spdb_sm2;
fn encrypt_and_decrypt () {
let msg = r#"hello world"#;
let secret = "ZTPkMS00ZNTP5NzPwNjAu";
// 加密
// M2YyZWFlOTU4MzBkZTUxMGQyOTNjNmUzYzA1ODg2NjM=
let encrypt_data = encrypt(msg, secret).unwrap();
// 解密
// decrypt_data == msg
let decrypt_data = decrypt(&encrypt_data, secret).unwrap();
}
Dependencies
~5MB
~86K SLoC