about summary refs log tree commit diff
path: root/kittybox-rs/examples/password-hasher.rs
blob: 2cba6069cbcc03e3e5ff58f82814638f0d072bbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::io::Write;

use argon2::{Argon2, password_hash::{rand_core::OsRng, PasswordHasher, PasswordHash, PasswordVerifier, SaltString}};

fn main() -> std::io::Result<()> {
    print!("Type a password: ");
    std::io::stdout().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();
    println!("{}", 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(())
}