From d0349d8b87a6e6638650e7a873285d212e1d1945 Mon Sep 17 00:00:00 2001 From: nikola-bozin-org Date: Wed, 22 May 2024 09:55:52 +0200 Subject: [PATCH] lifetime remove and refactor --- CHANGELOG.md | 4 +++ readme.md | 80 +--------------------------------------------------- src/lib.rs | 26 ++++++++--------- 3 files changed, 18 insertions(+), 92 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d83b9e9..886b555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.0 + +Complete refactoring and making EncryptionPassword more easier to manage by removing lifetime parameters. + # 1.1.2 Update readme.md diff --git a/readme.md b/readme.md index cfb03bf..a720ab1 100644 --- a/readme.md +++ b/readme.md @@ -12,83 +12,5 @@ or add it to the dependencies ```rust [dependencies] -password-encryptor = "1.1.2" -``` - -# Usage - -## Import - -```rust -use password_encryptor::{EncryptionData, PasswordEncryptor}; -``` - -## Example functions - -### Note: Make sure that `encryption_prefix` is the same for encryption and validation. If you dont want to use prefix, just pass `None` instead. - -```rust -pub fn encrypt_password(password: &str, salt: &str) -> String { - let encryptor = PasswordEncryptor::new(b"secret_key", Some("prefix_")); - let data = EncryptionData { - content: password, - salt, - }; - - let encrypted_password = encryptor.encrypt_password(&data); - match encrypted_password { - Ok(result) => result, - Err(e) => { - format!("Unable to encrypt password. {:?}", e) - } - } -} - -pub fn validate_password(password: &str, encrypted_password: &str, salt: &str) -> bool { - let encryptor = PasswordEncryptor::new(b"secret_key", Some("prefix_")); - let data = EncryptionData { - content: password, - salt, - }; - let is_valid_password = encryptor.validate_password(&data, encrypted_password); - is_valid_password.is_ok() -} -``` - -# Test cases - -```rust -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_encrypt_password() { - let password = "test_password"; - let salt = "random_salt"; - - let encrypted_password = encrypt_password(password, salt); - assert!(!encrypted_password.contains("Unable to encrypt password."), "Encryption should succeed without errors."); - } - - #[test] - fn test_validate_password_success() { - let password = "test_password"; - let salt = "random_salt"; - let encrypted_password = encrypt_password(password, salt); - - assert!(validate_password(password, encrypted_password.as_str(), salt), "Password validation should succeed."); - } - - #[test] - fn test_validate_password_failure() { - let password = "test_password"; - let wrong_password = "wrong_password"; - let salt = "random_salt"; - let encrypted_password = encrypt_password(password, salt); - - assert!(!validate_password(wrong_password, encrypted_password.as_str(), salt), "Password validation should fail with incorrect password."); - } -} - +password-encryptor = "2.0.0" ``` diff --git a/src/lib.rs b/src/lib.rs index 7615427..0992175 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,13 +12,13 @@ pub struct EncryptionData<'a> { } #[derive(Clone)] -pub struct PasswordEncryptor<'a> { - key: &'a [u8], - encryption_prefix: Option<&'a str>, +pub struct PasswordEncryptor { + key: Vec, + encryption_prefix: Option, } -impl<'a> PasswordEncryptor<'a> { - pub fn new(key: &'a [u8], encryption_prefix: Option<&'a str>) -> Self { +impl PasswordEncryptor { + pub fn new(key: Vec, encryption_prefix: Option) -> Self { Self { key, encryption_prefix, @@ -26,10 +26,10 @@ impl<'a> PasswordEncryptor<'a> { } } -impl<'a> PasswordEncryptor<'a> { +impl PasswordEncryptor { fn encrypt_into_base64url( &self, - key: &'a [u8], + key: &[u8], encryption_data: &EncryptionData, ) -> Result { let EncryptionData { content, salt } = encryption_data; @@ -49,8 +49,8 @@ impl<'a> PasswordEncryptor<'a> { } pub fn encrypt_password(&self, encryption_data: &EncryptionData) -> Result { - let encrypted = self.encrypt_into_base64url(self.key, encryption_data)?; - let final_prefix = self.encryption_prefix.unwrap_or(""); + let encrypted = self.encrypt_into_base64url(&self.key, encryption_data)?; + let final_prefix = self.encryption_prefix.clone().unwrap_or("".to_string()); Ok(format!("{final_prefix}{encrypted}")) } @@ -74,7 +74,7 @@ mod tests { #[test] fn test_successful_encryption() { - let encryptor = PasswordEncryptor::new(b"secret_key", None); + let encryptor = PasswordEncryptor::new(vec![1,2,3], None); let data = EncryptionData { content: "password123", salt: "salt", @@ -86,7 +86,7 @@ mod tests { #[test] fn test_validation_success() { - let encryptor = PasswordEncryptor::new(b"secret_key", None); + let encryptor = PasswordEncryptor::new(vec![1,2,3], None); let data = EncryptionData { content: "password123", salt: "salt", @@ -98,7 +98,7 @@ mod tests { #[test] fn test_validation_failure() { - let encryptor = PasswordEncryptor::new(b"secret_key", Some("prefix_")); + let encryptor = PasswordEncryptor::new(vec![1,2,3], Some("prefix_".to_string())); let data = EncryptionData { content: "password123", salt: "salt", @@ -111,7 +111,7 @@ mod tests { #[test] fn test_password_mismatch_error() { - let encryptor = PasswordEncryptor::new(b"secret_key", Some("prefix_")); + let encryptor = PasswordEncryptor::new(vec![1,2,3], Some("prefix_".to_string())); let data = EncryptionData { content: "password123", salt: "salt",