Skip to content

Commit

Permalink
lifetime remove and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-org committed May 22, 2024
1 parent 13ff4dd commit d0349d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
80 changes: 1 addition & 79 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ pub struct EncryptionData<'a> {
}

#[derive(Clone)]
pub struct PasswordEncryptor<'a> {
key: &'a [u8],
encryption_prefix: Option<&'a str>,
pub struct PasswordEncryptor {
key: Vec<u8>,
encryption_prefix: Option<String>,
}

impl<'a> PasswordEncryptor<'a> {
pub fn new(key: &'a [u8], encryption_prefix: Option<&'a str>) -> Self {
impl PasswordEncryptor {
pub fn new(key: Vec<u8>, encryption_prefix: Option<String>) -> Self {
Self {
key,
encryption_prefix,
}
}
}

impl<'a> PasswordEncryptor<'a> {
impl PasswordEncryptor {
fn encrypt_into_base64url(
&self,
key: &'a [u8],
key: &[u8],
encryption_data: &EncryptionData,
) -> Result<String> {
let EncryptionData { content, salt } = encryption_data;
Expand All @@ -49,8 +49,8 @@ impl<'a> PasswordEncryptor<'a> {
}

pub fn encrypt_password(&self, encryption_data: &EncryptionData) -> Result<String> {
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}"))
}

Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit d0349d8

Please sign in to comment.