Updated cryptography functions to be in-place
This commit is contained in:
@ -25,7 +25,9 @@ async fn update_account(
|
||||
|
||||
let field_value = spawn_blocking(move || {
|
||||
let cipher = Cipher::new(master_pass.as_bytes(), &salt);
|
||||
cipher.encrypt(field_value.as_bytes()).unwrap()
|
||||
let mut field = field_value.into_bytes();
|
||||
cipher.encrypt(&mut field);
|
||||
field
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
@ -23,8 +23,13 @@ async fn get_master_pass(
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??;
|
||||
let text = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`");
|
||||
let account =
|
||||
spawn_blocking(move || DecryptedAccount::from_account(account, &master_pass)).await??;
|
||||
|
||||
let text = format!(
|
||||
"Name:\n`{name}`\nLogin:\n`{}`\nPassword:\n`{}`",
|
||||
account.login, account.password
|
||||
);
|
||||
|
||||
ids.alter_message(
|
||||
&bot,
|
||||
|
@ -20,9 +20,14 @@ async fn get_master_pass(
|
||||
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
||||
|
||||
let account = spawn_blocking(move || {
|
||||
account::ActiveModel::from_unencrypted(user_id, name, &login, &password, &master_pass)
|
||||
DecryptedAccount {
|
||||
name,
|
||||
login,
|
||||
password,
|
||||
}
|
||||
.into_account(user_id, &master_pass)
|
||||
})
|
||||
.await??;
|
||||
.await?;
|
||||
account.insert(&db).await?;
|
||||
|
||||
ids.alter_message(&bot, "Success", deletion_markup(), None)
|
||||
|
@ -17,7 +17,7 @@ async fn encrypt_account(
|
||||
) {
|
||||
let name = account.name.clone();
|
||||
match spawn_blocking(move || account.into_account(user_id, &master_pass)).await {
|
||||
Ok(Ok(account)) => match account.insert(db).await {
|
||||
Ok(account) => match account.insert(db).await {
|
||||
Ok(_) => (),
|
||||
Err(_) => failed.lock().push(name),
|
||||
},
|
||||
|
@ -1,58 +1,8 @@
|
||||
//! Models to export and import the accounts
|
||||
|
||||
use crate::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DecryptedAccount {
|
||||
pub name: String,
|
||||
pub login: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl DecryptedAccount {
|
||||
/// Constructs `DecryptedAccount` by decrypting the provided account
|
||||
#[inline]
|
||||
pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result<Self> {
|
||||
let (login, password) = account.decrypt(master_pass)?;
|
||||
Ok(Self {
|
||||
name: account.name,
|
||||
login,
|
||||
password,
|
||||
})
|
||||
}
|
||||
|
||||
/// Constructs `ActiveModel` with eath field Set by encrypting `self`
|
||||
#[inline]
|
||||
pub fn into_account(
|
||||
self,
|
||||
user_id: u64,
|
||||
master_pass: &str,
|
||||
) -> crate::Result<account::ActiveModel> {
|
||||
account::ActiveModel::from_unencrypted(
|
||||
user_id,
|
||||
self.name,
|
||||
&self.login,
|
||||
&self.password,
|
||||
master_pass,
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Returns true if the account's fields are valid
|
||||
#[inline]
|
||||
pub fn validate(&self) -> bool {
|
||||
[
|
||||
self.name.as_str(),
|
||||
self.login.as_str(),
|
||||
self.password.as_str(),
|
||||
]
|
||||
.into_iter()
|
||||
.all(validate_field)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[repr(transparent)]
|
||||
pub struct User {
|
||||
pub accounts: Vec<DecryptedAccount>,
|
||||
|
11
src/utils.rs
11
src/utils.rs
@ -6,17 +6,6 @@ pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
}
|
||||
|
||||
/// Returns true if the field is valid
|
||||
#[inline]
|
||||
pub fn validate_field(field: &str) -> bool {
|
||||
if !(1..255).contains(&field.len()) {
|
||||
return false;
|
||||
}
|
||||
field
|
||||
.chars()
|
||||
.all(|char| !['`', '\\', '\n', '\t'].contains(&char))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub async fn name_from_hash(
|
||||
db: &DatabaseConnection,
|
||||
|
Reference in New Issue
Block a user