Skip to content

Commit

Permalink
Update main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloTheFox authored Feb 17, 2024
1 parent 30eb225 commit 2039ba2
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use argon2::{password_hash::SaltString, Algorithm, Argon2, Params, PasswordHasher, Version};
use colored::Colorize;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use rand::rngs::StdRng;
use rand::SeedableRng;
use rand::{distributions::Alphanumeric, Rng};
use rand_core::OsRng;
use std::fmt;
use thiserror::Error;
Expand All @@ -19,6 +21,7 @@ impl std::error::Error for ArgonError {}

#[derive(Error, Debug)]
pub enum MyError {
// ... other variants
#[error("Error hashing password: {0}")]
HashingError(ArgonError),
#[error("Other error: {0}")]
Expand All @@ -32,7 +35,8 @@ const OUTPUT_LEN: usize = 64;

#[tokio::main]
async fn main() -> Result<(), MyError> {
let passwords: Vec<String> = (0..50).map(|_| generate_password(16)).collect();
let passwords: Vec<String> =
futures::future::join_all((0..50).map(|_| generate_password(16))).await;

// Define Argon2 parameters
let params_result = Params::new(MEMORY_COST, TIME_COST, PARALLELISM, Some(OUTPUT_LEN));
Expand Down Expand Up @@ -71,6 +75,7 @@ async fn main() -> Result<(), MyError> {
Err(e) => println!("Failed to hash password: {}", e),
}
}
// Log success message
println!("{}", "[LOG] Passwords hashed successfully".green());
}
Err(e) => {
Expand All @@ -81,23 +86,27 @@ async fn main() -> Result<(), MyError> {
Ok(())
}

fn generate_password(length: u8) -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(length as usize)
.map(char::from)
.collect()
async fn generate_password(length: u8) -> String {
let rng = StdRng::from_entropy(); // create a new StdRng
task::spawn(async move {
rng.sample_iter(&Alphanumeric)
.take(length as usize)
.map(char::from)
.collect::<String>()
})
.await
.unwrap()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_generate_password() {
let password = generate_password(16);
assert_eq!(password.len(), 16);
assert!(password.chars().all(|c| c.is_alphanumeric()));
#[tokio::test]
async fn test_generate_password() {
let password_future = generate_password(16);
let password = password_future.await;
assert!(password.len() == 16 && password.chars().all(|c| c.is_alphanumeric()));
}

#[test]
Expand All @@ -113,7 +122,7 @@ mod tests {
let params = Params::new(MEMORY_COST, TIME_COST, PARALLELISM, Some(OUTPUT_LEN)).unwrap();
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
let salt = SaltString::generate(&mut OsRng);
let result = argon2.hash_password(password.as_bytes(), &salt);
let result = argon2.hash_password(password.await.as_bytes(), &salt);
assert!(result.is_ok());
}
}

0 comments on commit 2039ba2

Please sign in to comment.