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::types::{KeyPair, PrivateKey, PublicKey};
use std::str;
use std::u8;
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 string = String::from(utf);
return String::from(string);
@ -24,46 +25,44 @@ pub fn private_key_from_sring (pk: String) -> PrivateKey {
return imported
}
pub fn generate () -> (String, String, KeyPair) {
let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap();
let kp = ntru::generate_key_pair(&DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap();
let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap();
let kp = ntru::generate_key_pair(&DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap();
// extracting public and private key from kp
let pub_key = KeyPair::get_public(&kp);
let private_key = KeyPair::get_private(&kp);
let pub_key = KeyPair::get_public(&kp);
let private_key = KeyPair::get_private(&kp);
// getting pub and priv key params for exporting
let pub_key_params = KeyPair::get_params(&kp).unwrap();
let private_key_params = PrivateKey::get_params(&private_key).unwrap();
let pub_key_params = KeyPair::get_params(&kp).unwrap();
let private_key_params = PrivateKey::get_params(&private_key).unwrap();
// exporting
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);
// converting to string
let pub_key_string = key_to_string(&pub_key_exported);
let private_key_string = key_to_string(&private_key_exported);
/*
* It was a test
*
let pub_key_string = u8_to_string(&pub_key_exported);
let private_key_string = u8_to_string(&private_key_exported);
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 )
}
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();
@ -75,9 +74,15 @@ pub fn main () {
let bo0 = initial_pub == &final_pub;
let bo1 = initial_priv == &final_priv;
/*
print!("Pub : {}, Priv : {}", bo0, bo1);
assert_eq!(initial_pub, &final_pub);
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 encrypting;
mod tests;
fn main() {
encrypting::encryption::main()
encrypting::encryption::test();
/*
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;