-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code cleanup done, minor issues fixed
- Loading branch information
man2k
committed
Sep 15, 2023
1 parent
1814ec1
commit 9129a9e
Showing
15 changed files
with
313 additions
and
512 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,26 @@ | ||
use crate::commands::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc}; | ||
use crate::utilities::passargon::passargon; | ||
use super::super::utilities::passargon; | ||
use super::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc, Algorithms, Error}; | ||
use block_modes::BlockMode; | ||
use hex::encode; | ||
use std::vec; | ||
|
||
#[tauri::command(async)] | ||
pub fn encrypttext(text_str: String, password: String, algo: usize) -> Result<String, String> { | ||
pub fn encrypttext(text_str: String, password: String, algo: usize) -> Result<String, Error> { | ||
let plaintext = text_str.as_bytes(); | ||
let pos = plaintext.len(); | ||
let mut buffer: Vec<u8> = vec![0u8; pos + 100]; | ||
buffer[..pos].copy_from_slice(&plaintext); | ||
let (iv, key) = passargon(password, algo / 8).unwrap(); | ||
// println!("iv : {:?} \nkey : {:?}", iv, key); | ||
if algo == 128 { | ||
let cipher = Aes128Cbc::new_from_slices(&key, &iv).unwrap(); | ||
|
||
let ciphertext = match cipher.encrypt(&mut buffer, pos) { | ||
Err(_) => return Err("encryption failed".to_string()), | ||
Ok(cipt) => cipt, | ||
}; | ||
let finalchipher = [ciphertext, &iv].concat(); | ||
Ok(encode(finalchipher).into()) | ||
} else if algo == 192 { | ||
let cipher = Aes192Cbc::new_from_slices(&key, &iv).unwrap(); | ||
let ciphertext = match cipher.encrypt(&mut buffer, pos) { | ||
Err(_) => return Err("encryption failed".to_string()), | ||
Ok(cipt) => cipt, | ||
}; | ||
let finalchipher = [ciphertext, &iv].concat(); | ||
Ok(encode(finalchipher).into()) | ||
} else if algo == 256 { | ||
let cipher = Aes256Cbc::new_from_slices(&key, &iv).unwrap(); | ||
let ciphertext = match cipher.encrypt(&mut buffer, pos) { | ||
Err(_) => return Err("encryption failed".to_string()), | ||
Ok(cipt) => cipt, | ||
}; | ||
let finalchipher = [ciphertext, &iv].concat(); | ||
let cipher: Algorithms = match algo { | ||
128 => Algorithms::AES128(Aes128Cbc::new_from_slices(&key, &iv).unwrap()), | ||
192 => Algorithms::AES192(Aes192Cbc::new_from_slices(&key, &iv).unwrap()), | ||
256 => Algorithms::AES256(Aes256Cbc::new_from_slices(&key, &iv).unwrap()), | ||
_ => todo!(), | ||
}; | ||
|
||
Ok(encode(finalchipher).into()) | ||
} else { | ||
Ok("failed".into()) | ||
} | ||
let ciphertext = cipher.encrypt(&mut buffer, pos)?; | ||
let finalchipher = [ciphertext, iv.to_vec()].concat(); | ||
|
||
Ok(encode(finalchipher)) | ||
} |
Oops, something went wrong.