Password hashing helpers

This commit is contained in:
Raphaël Thériault 2020-08-12 00:12:22 -04:00
parent 4797c7c881
commit 8a6824b7c0
4 changed files with 22 additions and 1 deletions

1
Cargo.lock generated
View File

@ -352,6 +352,7 @@ dependencies = [
"chrono 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.10.30 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-argon2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -19,12 +19,13 @@ anyhow = "1.0.32"
askama = "0.10.3"
chrono = { version = "0.4.13", features = ["serde"] }
futures = "0.3.5"
rand = "0.7.3"
rust-argon2 = "0.8.2"
serde = { version = "1.0.115", features = ["derive"] }
serde_json = "1.0.57"
sqlx = { version = "0.4.0-beta.1", features = ["chrono", "macros", "migrate", "offline", "runtime-tokio", "sqlite"], default-features = false }
structopt = "0.3.16"
tokio = { version = "0.2.22", features = ["blocking", "fs", "rt-core"] }
tokio = { version = "0.2.22", features = ["blocking", "fs", "rt-threaded"] }
tracing = "0.1.19"
tracing-futures = "0.2.4"
tracing-subscriber = "0.2.11"

18
src/auth.rs Normal file
View File

@ -0,0 +1,18 @@
use anyhow::Result;
use argon2::Config;
use rand::Rng;
use tokio::task;
// TODO: Allow custom configuration
async fn hash(password: Vec<u8>) -> Result<String> {
let config = Config::default();
Ok(task::spawn_blocking(move || {
let salt: [u8; 16] = rand::thread_rng().gen();
argon2::hash_encoded(&password, &salt[..], &config)
})
.await??)
}
async fn verify(encoded: String, password: Vec<u8>) -> Result<bool> {
Ok(task::spawn_blocking(move || argon2::verify_encoded(&encoded, &password)).await??)
}

View File

@ -1,3 +1,4 @@
mod auth;
mod config;
mod db;
mod routes;