make basic ntru encrypting api

This commit is contained in:
horhik 2020-09-12 22:46:14 +03:00
parent 754edd4725
commit 0940192faa
4 changed files with 43 additions and 31 deletions

View file

@ -2,11 +2,12 @@ use ntru::rand::RNG_DEFAULT;
use ntru::encparams::{DEFAULT_PARAMS_256_BITS}; use ntru::encparams::{DEFAULT_PARAMS_256_BITS};
use ntru::types::{KeyPair, PrivateKey, PublicKey}; use ntru::types::{KeyPair, PrivateKey, PublicKey};
use std::str; use std::str;
use std::u8;
extern crate ntru; extern crate ntru;
pub fn key_to_string(key: &Box<[u8]>) -> String { pub fn u8_to_string(key: &Box<[u8]>) -> String {
let utf = unsafe {str::from_utf8_unchecked(&key)}; let utf = unsafe {str::from_utf8_unchecked(&key)};
let string = String::from(utf); let string = String::from(utf);
return String::from(string); return String::from(string);
@ -24,7 +25,6 @@ pub fn private_key_from_sring (pk: String) -> PrivateKey {
return imported return imported
} }
pub fn generate () -> (String, String, KeyPair) { pub fn generate () -> (String, String, KeyPair) {
let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap(); let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap();
let kp = ntru::generate_key_pair(&DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap(); let kp = ntru::generate_key_pair(&DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap();
@ -38,32 +38,31 @@ pub fn generate () -> (String, String, KeyPair) {
let pub_key_exported: Box<[u8]> = PublicKey::export(&pub_key, &pub_key_params); let pub_key_exported: Box<[u8]> = PublicKey::export(&pub_key, &pub_key_params);
let private_key_exported: Box<[u8]> = PrivateKey::export(&private_key, &private_key_params); let private_key_exported: Box<[u8]> = PrivateKey::export(&private_key, &private_key_params);
// converting to string // converting to string
let pub_key_string = key_to_string(&pub_key_exported); let pub_key_string = u8_to_string(&pub_key_exported);
let private_key_string = key_to_string(&private_key_exported); let private_key_string = u8_to_string(&private_key_exported);
/*
* It was a test
*
let xp = &PublicKey::import(&pub_key_exported);
let boo = xp == pub_key;
let value = from_utf8(&pub_key_exported).unwrap();
let strVal = String::from(value);
print!(" without converting \n {}", xp == pub_key );
assert_eq!(xp, pub_key);
let newbytes = strVal.into_bytes();
let newkey = PublicKey::import(&newbytes);
print!(" with converting \n {}", &newkey == pub_key);
assert_eq!(&newkey , pub_key);
*/
return (pub_key_string, private_key_string, kp ) return (pub_key_string, private_key_string, kp )
} }
pub fn encrypt_message (msg:String, key: PublicKey ) -> String {
pub fn main () { let _msg = msg.into_bytes();
let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap();
let encrypted = ntru::encrypt(&_msg, &key, &DEFAULT_PARAMS_256_BITS,
&rand_ctx).unwrap();
let message = u8_to_string(&encrypted);
return message
}
pub fn decrypt_message(msg: String, kp: &KeyPair) -> String {
let encrypted_message = msg.into_bytes();
let decrypted = ntru::decrypt(&encrypted_message, &kp, &DEFAULT_PARAMS_256_BITS).unwrap();
let decrypted_string = u8_to_string(&decrypted);
return decrypted_string
}
pub fn test () {
let (public, private, key_pair) = generate(); let (public, private, key_pair) = generate();
@ -75,9 +74,15 @@ pub fn main () {
let bo0 = initial_pub == &final_pub; let bo0 = initial_pub == &final_pub;
let bo1 = initial_priv == &final_priv; let bo1 = initial_priv == &final_priv;
/*
print!("Pub : {}, Priv : {}", bo0, bo1); print!("Pub : {}, Priv : {}", bo0, bo1);
assert_eq!(initial_pub, &final_pub); assert_eq!(initial_pub, &final_pub);
assert_eq!(initial_priv, &final_priv); assert_eq!(initial_priv, &final_priv);
*/
let encrypted_message = encrypt_message(String::from("hello god"), final_pub);
let decrypted_message = decrypt_message(encrypted_message, &key_pair);
print!("Msg was decrypted: {}", decrypted_message);
} }

View file

@ -1,9 +1,10 @@
mod cli; mod cli;
mod encrypting; mod encrypting;
mod tests;
fn main() { fn main() {
encrypting::encryption::main() encrypting::encryption::test();
/* /*
let (one, two) = keys; let (one, two) = keys;

View file

@ -0,0 +1,4 @@
pub fn main () {
}

2
src/tests/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod encrypting_tests;