about summary refs log tree commit diff
path: root/kittybox-rs/examples
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-09-19 17:33:58 +0300
committerVika <vika@fireburn.ru>2022-09-19 17:33:58 +0300
commit477274d099cd07f2fa1bc60e0acaf3af8d9830ae (patch)
treec917973dd29c090e55d624aeaf0a999ea1fb6059 /kittybox-rs/examples
parentfe116a8117bf398a83bb25efbd5c866fc33c3d4f (diff)
downloadkittybox-477274d099cd07f2fa1bc60e0acaf3af8d9830ae.tar.zst
Add test password hasher
Diffstat (limited to 'kittybox-rs/examples')
-rw-r--r--kittybox-rs/examples/password-hasher.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/kittybox-rs/examples/password-hasher.rs b/kittybox-rs/examples/password-hasher.rs
new file mode 100644
index 0000000..2cba606
--- /dev/null
+++ b/kittybox-rs/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<()> {
+    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(())
+}