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

View File

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

View File

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

View File

@ -1,9 +1,10 @@
mod cli;
mod encrypting;
use encrypting::{KeyPair, generate_kp, encrypt_message, decrypt_message};
fn main() {
/*
/*
let (one, two) = keys;
let value =String::from_utf8_lossy(&*one);

0
tests/chat_api_tests.rs Normal file
View File

View File

@ -1,6 +1,4 @@
use hole::encrypting::encryption::*;
use std::collections::HashMap;
use ntru::types::{KeyPair, PrivateKey, PublicKey};
#[test]
@ -44,11 +42,30 @@ fn it_making_correct_decrypt () {
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 dec_msg = decrypt_message(enc_msg, &keypair);
decrypt_message(enc_msg, &keypair, &mut 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)
}