Don't remake salt & argon2 each time

status
~erin 2023-04-02 20:58:23 -04:00
parent 63cce3cbc1
commit d5d5841955
Signed by: erin
GPG Key ID: 9A8E308CEFA37A47
2 changed files with 18 additions and 6 deletions

0
README.md Normal file
View File

View File

@ -132,11 +132,9 @@ async fn build(msg: Message, address: SocketAddr) {
}
impl Message {
async fn authenticate(&self, passwd: String) -> bool {
async fn authenticate(&self, passwd: String, salt: SaltString, argon2: Argon2<'_>) -> bool {
match &self.authentication {
Some(auth) => {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
let password_hash = argon2
.hash_password(auth.as_bytes(), &salt)
.unwrap()
@ -160,7 +158,13 @@ impl Message {
}
}
async fn process_socket(mut socket: TcpStream, address: SocketAddr, auth: AuthConfig) {
async fn process_socket(
mut socket: TcpStream,
address: SocketAddr,
auth: AuthConfig,
salt: SaltString,
argon2: Argon2<'_>,
) {
let (reader, writer) = socket.split();
let mut reader = BufReader::new(reader);
@ -183,7 +187,10 @@ async fn process_socket(mut socket: TcpStream, address: SocketAddr, auth: AuthCo
if auth.authenticate {
match &auth.password {
Some(pass) => {
if json.authenticate(pass.clone()).await {
if json
.authenticate(pass.clone(), salt.clone(), argon2.clone())
.await
{
tokio::spawn(async move {
build(json, address).await;
});
@ -206,12 +213,17 @@ async fn main() -> io::Result<()> {
let listener = TcpListener::bind(&addr).await?;
info!("Listening on: <green>{}<//>", addr);
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
loop {
match listener.accept().await {
Ok((socket, addr)) => {
let auth = config.auth.clone();
let m_salt = salt.clone();
let m_argon2 = argon2.clone();
tokio::spawn(async move {
process_socket(socket, addr, auth).await;
process_socket(socket, addr, auth, m_salt, m_argon2).await;
});
}
Err(e) => error!("couldn't get client: {:?}", e),