From 8d4b969419e28efe5630c1e0ad1fd37421e2064c Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Feb 2024 18:59:21 +0000 Subject: [PATCH] Implement ServerPadlockGenerator --- src/auth.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/auth.rs b/src/auth.rs index fc36db1..2f8d389 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -8,6 +8,7 @@ use rand::{ thread_rng, }; use secrecy::ExposeSecret; +use sha2::Sha256; use thiserror::Error; use time::{macros::format_description, OffsetDateTime}; use tokio::sync::Mutex; @@ -85,13 +86,25 @@ pub struct ServerPadlockGenerator { } impl ServerPadlockGenerator { + const HASH_LEN: usize = 32; + pub fn new(secret: PadlockGenerationSecret) -> Self { Self { secret } } + #[instrument] + pub fn generate_hash() -> ServerHash { + ServerHash(Alphanumeric.sample_string(&mut thread_rng(), Self::HASH_LEN)) + } + #[instrument] pub fn generate_padlock(&self, server_hash: &ServerHash) -> ServerPadlock { - todo!() + let mut hmac: Hmac = Hmac::new_from_slice(self.secret.0.expose_secret()) + .expect("HMAC should accept key of any length"); + + hmac.update(server_hash.0.as_bytes()); + + BASE64_STANDARD.encode(hmac.finalize().into_bytes()).into() } }