mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-15 04:21:22 +00:00
refeat: use standard engine encode & decode
- instead of the deprecated base64 encode & decode
This commit is contained in:
parent
b730c5def5
commit
12e86d5dff
1 changed files with 14 additions and 10 deletions
|
|
@ -4,6 +4,8 @@ use argon2::{
|
|||
Argon2, PasswordHasher as ArgonPasswordHasher, PasswordVerifier as ArgonPasswordVerifier,
|
||||
};
|
||||
use async_compat::CompatExt;
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose;
|
||||
use futures::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use rand::Rng;
|
||||
use secrecy::ExposeSecret;
|
||||
|
|
@ -123,14 +125,14 @@ pub async fn encrypt_password(
|
|||
})
|
||||
.await??;
|
||||
|
||||
Ok(base64::encode(hash))
|
||||
Ok(general_purpose::STANDARD.encode(hash))
|
||||
}
|
||||
|
||||
pub async fn decrypt_password(
|
||||
password_cipher_text: String,
|
||||
key: String,
|
||||
) -> Result<String, ServiceError> {
|
||||
let input = base64::decode(password_cipher_text)?;
|
||||
let input = general_purpose::STANDARD.decode(password_cipher_text)?;
|
||||
let plain = tokio::task::spawn_blocking(move || {
|
||||
let aes_key_nonce = convert_key_aes256(&key);
|
||||
|
||||
|
|
@ -164,7 +166,7 @@ pub async fn encrypt_password_age(
|
|||
|
||||
encrypt_writer.close().await?;
|
||||
|
||||
Ok(base64::encode(encrypt_buffer))
|
||||
Ok(general_purpose::STANDARD.encode(encrypt_buffer))
|
||||
}
|
||||
|
||||
#[deprecated(note = "Too slow, use AES instead")]
|
||||
|
|
@ -172,7 +174,7 @@ pub async fn decrypt_password_age(
|
|||
password_encrypted: &str,
|
||||
key: &str,
|
||||
) -> Result<String, ServiceError> {
|
||||
let encrypted = base64::decode(password_encrypted)?;
|
||||
let encrypted = general_purpose::STANDARD.decode(password_encrypted)?;
|
||||
|
||||
let decryptor = match age::Decryptor::new_async(&encrypted[..]).await? {
|
||||
age::Decryptor::Passphrase(d) => d,
|
||||
|
|
@ -263,14 +265,14 @@ pub async fn encrypt_password_with_recipients(
|
|||
)
|
||||
.await?;
|
||||
|
||||
Ok(base64::encode(encrypt_buffer))
|
||||
Ok(general_purpose::STANDARD.encode(encrypt_buffer))
|
||||
}
|
||||
|
||||
pub async fn decrypt_password_with_private_key(
|
||||
password_encrypted: &str,
|
||||
key: &str,
|
||||
) -> Result<String, ServiceError> {
|
||||
let encrypted = base64::decode(password_encrypted)?;
|
||||
let encrypted = general_purpose::STANDARD.decode(password_encrypted)?;
|
||||
|
||||
let mut decrypt_buffer = Vec::new();
|
||||
|
||||
|
|
@ -338,6 +340,8 @@ pub async fn decrypt_file_with_private_key_as_buffer<P: AsRef<Path>>(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
|
||||
#[test]
|
||||
fn test_random_12_char_string() {
|
||||
for _ in 0..1000 {
|
||||
|
|
@ -406,7 +410,7 @@ mod tests {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(base64::decode(encrypted).is_ok());
|
||||
assert!(general_purpose::STANDARD.decode(encrypted).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -433,7 +437,7 @@ mod tests {
|
|||
#[allow(deprecated)]
|
||||
let encrypted = super::encrypt_password_age(PASSWORD, KEY).await.unwrap();
|
||||
|
||||
assert!(base64::decode(encrypted).is_ok());
|
||||
assert!(general_purpose::STANDARD.decode(encrypted).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -466,7 +470,7 @@ mod tests {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(base64::decode(encrypted).is_ok());
|
||||
assert!(general_purpose::STANDARD.decode(encrypted).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -480,7 +484,7 @@ mod tests {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(base64::decode(encrypted).is_ok());
|
||||
assert!(general_purpose::STANDARD.decode(encrypted).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue