add skip decrypt function

This commit is contained in:
horhik 2020-09-14 11:50:18 +03:00
parent 1f952100e3
commit 95baef1db7
8 changed files with 48 additions and 10 deletions

11
src/chat/chat_api_base.rs Normal file
View file

@ -0,0 +1,11 @@
pub struct Message {
sender_id: i32,
message: String,
}
pub fn show_message () {
}
pub fn main () {}

1
src/chat/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod chat_api_base;

View file

@ -1,6 +1,6 @@
use ntru::rand::RNG_DEFAULT; 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}; pub use ntru::types::{KeyPair, PrivateKey, PublicKey};
use std::str; use std::str;
use std::u8; use std::u8;
extern crate ntru; extern crate ntru;
@ -67,12 +67,17 @@ pub fn encrypt_message (msg:String, key: &PublicKey ) -> String {
return message return message
} }
pub fn decrypt_message(msg: String, kp: &KeyPair) -> String { pub fn decrypt_message(msg: String, kp: &KeyPair, final_msg: &mut String) {
let encrypted_message = msg.into_bytes(); let encrypted_message = msg.into_bytes();
let decrypted = ntru::decrypt(&encrypted_message, &kp, &DEFAULT_PARAMS_256_BITS).unwrap(); let decrypted = ntru::decrypt(&encrypted_message, &kp, &DEFAULT_PARAMS_256_BITS);
let decrypted_string = u8_to_string(&decrypted); match decrypted {
return decrypted_string Ok(res) => {
*final_msg = u8_to_string(&res)
},
Err(e) => {}
}
} }

View file

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

View file

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

View file

@ -1,5 +1,6 @@
mod cli; mod cli;
mod encrypting; mod encrypting;
use encrypting::{KeyPair, generate_kp, encrypt_message, decrypt_message};
fn main() { fn main() {

0
tests/chat_api_tests.rs Normal file
View file

View file

@ -1,6 +1,4 @@
use hole::encrypting::encryption::*; use hole::encrypting::encryption::*;
use std::collections::HashMap;
use ntru::types::{KeyPair, PrivateKey, PublicKey};
#[test] #[test]
@ -44,11 +42,30 @@ fn it_making_correct_decrypt () {
let msg = "👻: it's a ghost. Ghost is unexpectively bloodthirsty".to_string(); 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 my_msg = msg.clone();
let mut dec_msg = "".to_string();
let enc_msg = encrypt_message(msg, public); let enc_msg = encrypt_message(msg, public);
let dec_msg = decrypt_message(enc_msg, &keypair); decrypt_message(enc_msg, &keypair, &mut dec_msg);
assert_eq!(my_msg, dec_msg) assert_eq!(my_msg, dec_msg)
} }
#[test]
fn it_skipping_decrypt () {
let (_, _, keypair) = generate_kp();
let (_, _, wrong_keypair) = generate_kp();
let public = KeyPair::get_public(&keypair);
let msg = "👻: it's a ghost. Ghost is unexpectively bloodthirsty".to_string();
let my_msg = msg.clone();
let mut dec_msg = "".to_string();
let enc_msg = encrypt_message(msg, public);
decrypt_message(enc_msg, &wrong_keypair, &mut dec_msg);
assert_ne!(my_msg, dec_msg)
}