neuedu-cryptos 0.1.6

包含国密算法的密码库。 A library include SM3, SM4 (Chinese ShangMi) crypto algos.
Documentation

此密码库实现了中华人民共和国国家密码管理局发布的商用密码算法。

This cryptographic library implements the ShangMi algorithms which issued by the State Cryptography Administration of the People's Republic of China.

SM3杂凑值计算示例(SM3 Hash Calculation Example)

use neuedu_cryptos::hash_functions::SM3;

let mut sm3 = SM3::new();
sm3.update("abc".as_bytes());
let digest = sm3.r#final();

assert_eq!(digest, [
0x66, 0xC7, 0xF0, 0xF4, 0x62, 0xEE, 0xED, 0xD9,
0xD1, 0xF2, 0xD4, 0x6B, 0xDC, 0x10, 0xE4, 0xE2,
0x41, 0x67, 0xC4, 0x87, 0x5C, 0xF2, 0xF7, 0xA2,
0x29, 0x7D, 0xA0, 0x2B, 0x8F, 0x4B, 0xA8, 0xE0,
]);

SM4加密示例(SM4 Encryption Example)

use neuedu_cryptos::block_ciphers::{Operation, SM4};

const KEY: [u8; 16] = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
];
const PLAIN_MESSAGE: [u8; 16] = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
];
let mut sm4 = SM4::new(KEY, Operation::Encrypt, None, None);

let mut cipher_message = Vec::<u8>::new();
cipher_message.extend(&sm4.update(&PLAIN_MESSAGE));
cipher_message.extend(&sm4.r#final());

assert_eq!(cipher_message, [
0x68, 0x1E, 0xDF, 0x34, 0xD2, 0x06, 0x96, 0x5E,
0x86, 0xB3, 0xE9, 0x4F, 0x53, 0x6E, 0x42, 0x46,
]);

SM4解密示例(SM4 Decryption Example)

use neuedu_cryptos::block_ciphers::{Operation, SM4};

const KEY: [u8; 16] = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
];
const CIPHER_MESSAGE: [u8; 16] = [
0x68, 0x1E, 0xDF, 0x34, 0xD2, 0x06, 0x96, 0x5E,
0x86, 0xB3, 0xE9, 0x4F, 0x53, 0x6E, 0x42, 0x46,
];
let mut sm4 = SM4::new(KEY, Operation::Decrypt, None, None);

let mut plain_message = Vec::<u8>::new();
plain_message.extend(&sm4.update(&CIPHER_MESSAGE));
plain_message.extend(&sm4.r#final());

assert_eq!(plain_message, [
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
]);