From 0617663b249f9ca488e5de652108b17d67fbaf45 Mon Sep 17 00:00:00 2001 From: Vika Date: Sat, 29 Jul 2023 21:59:56 +0300 Subject: Moved the entire Kittybox tree into the root --- examples/password-hasher.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/password-hasher.rs (limited to 'examples/password-hasher.rs') diff --git a/examples/password-hasher.rs b/examples/password-hasher.rs new file mode 100644 index 0000000..f11c591 --- /dev/null +++ b/examples/password-hasher.rs @@ -0,0 +1,33 @@ +use std::io::Write; + +use argon2::{Argon2, password_hash::{rand_core::OsRng, PasswordHasher, PasswordHash, PasswordVerifier, SaltString}}; + +fn main() -> std::io::Result<()> { + eprint!("Type a password: "); + std::io::stderr().flush().unwrap(); + let password = { + let mut s = String::new(); + std::io::stdin().read_line(&mut s)?; + + s + }; + + let salt = SaltString::generate(&mut OsRng); + let argon2 = Argon2::default(); + //eprintln!("{}", password.trim()); + let password_hash = argon2.hash_password(password.trim().as_bytes(), &salt) + .expect("Hashing a password should not error out") + .serialize(); + + println!("{}", password_hash.as_str()); + + assert!(Argon2::default() + .verify_password( + password.trim().as_bytes(), + &PasswordHash::new(password_hash.as_str()) + .expect("Password hash should be valid") + ).is_ok() + ); + + Ok(()) +} -- cgit 1.4.1