add tests for encrypting

This commit is contained in:
horhik 2020-09-13 14:03:23 +03:00
parent 0940192faa
commit 151cade1fa
6 changed files with 73 additions and 35 deletions

View file

@ -25,7 +25,8 @@ pub fn private_key_from_sring (pk: String) -> PrivateKey {
return imported
}
pub fn generate () -> (String, String, KeyPair) {
pub fn generate_kp () -> (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();
// extracting public and private key from kp
@ -40,16 +41,28 @@ pub fn generate () -> (String, String, KeyPair) {
// converting to string
let pub_key_string = u8_to_string(&pub_key_exported);
let private_key_string = u8_to_string(&private_key_exported);
return (pub_key_string, private_key_string, kp )
}
pub fn encrypt_message (msg:String, key: PublicKey ) -> String {
pub fn kp_from_string (public: String,private: String) -> KeyPair {
let pub_key = public_key_from_sring(public);
let priv_key = private_key_from_sring(private);
let keypair = KeyPair::new(priv_key, pub_key);
return keypair;
}
pub fn encrypt_message (msg:String, key: &PublicKey ) -> String {
// to bytes
let _msg = msg.into_bytes();
//encrypting
let rand_ctx = ntru::rand::init(&RNG_DEFAULT).unwrap();
let encrypted = ntru::encrypt(&_msg, &key, &DEFAULT_PARAMS_256_BITS,
&rand_ctx).unwrap();
//to string
let message = u8_to_string(&encrypted);
return message
}
@ -62,27 +75,4 @@ pub fn decrypt_message(msg: String, kp: &KeyPair) -> String {
}
pub fn test () {
let (public, private, key_pair) = generate();
let initial_pub = KeyPair::get_public(&key_pair);
let initial_priv = KeyPair::get_private(&key_pair);
let final_pub = public_key_from_sring(public);
let final_priv = private_key_from_sring(private);
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);
}

2
src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod encrypting;
pub use encrypting::*;

View file

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

View file

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

View file

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

54
tests/encrypting_tests.rs Normal file
View file

@ -0,0 +1,54 @@
use hole::encrypting::encryption::*;
use std::collections::HashMap;
use ntru::types::{KeyPair, PrivateKey, PublicKey};
#[test]
fn it_converting_pubic_key () {
let (public, private, keypair) = generate_kp();
let initial_pub = KeyPair::get_public(&keypair);
let final_pub = public_key_from_sring(public);
let pub_eq = initial_pub == &final_pub;
assert_eq!(initial_pub, &final_pub);
}
#[test] fn it_converting_private_key () {
let (public, private, keypair) = generate_kp();
let initial_priv = KeyPair::get_private(&keypair);
let final_priv = private_key_from_sring(private);
let priv_eq = initial_priv == &final_priv;
assert_eq!(initial_priv, &final_priv);
}
#[test]
fn it_making_same_keypair () {
let (public, private, keypair) = generate_kp();
let new_keypair = kp_from_string(public, private);
assert_eq!(new_keypair, keypair);
}
#[test]
fn it_making_correct_decrypt () {
let (_, _, keypair) = generate_kp();
let public = KeyPair::get_public(&keypair);
let msg = "👻: it's a ghost. Ghost is unexpectively bloodthirsty".to_string();
let my_msg = "👻: it's a ghost. Ghost is unexpectively bloodthirsty".to_string();
let enc_msg = encrypt_message(msg, public);
let dec_msg = decrypt_message(enc_msg, &keypair);
assert_eq!(my_msg, dec_msg)
}