From f94ceea2e3dff6dfcfb017fc511a6f48450c7aa8 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 26 Aug 2026 11:52:39 +0200 Subject: [PATCH] Refactored crypto --- src/{signature.rs => crypto.rs} | 0 src/main.rs | 2 +- src/protocol/initialize.rs | 8 ++++---- src/protocol/message.rs | 14 +++++++------- src/server.rs | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) rename src/{signature.rs => crypto.rs} (100%) diff --git a/src/signature.rs b/src/crypto.rs similarity index 100% rename from src/signature.rs rename to src/crypto.rs diff --git a/src/main.rs b/src/main.rs index bacef3d..1dc0627 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ +pub mod crypto; pub mod data; pub mod protocol; pub mod server; -pub mod signature; pub mod types; use std::{ diff --git a/src/protocol/initialize.rs b/src/protocol/initialize.rs index 650cc48..7b0140e 100644 --- a/src/protocol/initialize.rs +++ b/src/protocol/initialize.rs @@ -66,7 +66,7 @@ impl UserConnections { return Err(anyhow::anyhow!("Client's hostname wasn't correct")); } - let Ok(public_key) = crate::signature::from_string(&public_key_string) else { + let Ok(public_key) = crate::crypto::from_string(&public_key_string) else { send_socket( &mut socket, &ClientMethod::Error { @@ -81,7 +81,7 @@ impl UserConnections { if public_key .verify_strict( format!("{timestamp}@{hostname}").as_bytes(), - &crate::signature::from_string_sig(&signature)?, + &crate::crypto::from_string_sig(&signature)?, ) .is_err() { @@ -100,8 +100,8 @@ impl UserConnections { send_socket( &mut socket, &ClientMethod::Initialized { - public_key: crate::signature::to_string(&server.key.verifying_key()), - signature: crate::signature::to_string_sig(&server.key.sign( + public_key: crate::crypto::to_string(&server.key.verifying_key()), + signature: crate::crypto::to_string_sig(&server.key.sign( format!("{server_timestamp}@{hostname}@{public_key_string}").as_bytes(), )), diff --git a/src/protocol/message.rs b/src/protocol/message.rs index 3c4b2c8..ec96e43 100644 --- a/src/protocol/message.rs +++ b/src/protocol/message.rs @@ -24,13 +24,13 @@ pub async fn send_message( ); } - let server_pubkey_string = crate::signature::to_string(&server.key.verifying_key()); + let server_pubkey_string = crate::crypto::to_string(&server.key.verifying_key()); let signed_string = format!( "{}@{}@{}", message.timestamp, server_pubkey_string, message.content ); - let signature = crate::signature::from_string_sig(&message.signature) + let signature = crate::crypto::from_string_sig(&message.signature) .map_err(|_| anyhow::anyhow!("Invalid signature encoding"))?; verifying_key @@ -39,7 +39,7 @@ pub async fn send_message( let stored = StoredMessage { id: uuid::Uuid::new_v4().to_string(), - author: crate::signature::to_string(&verifying_key), + author: crate::crypto::to_string(&verifying_key), is_edited: false, data: message, }; @@ -92,18 +92,18 @@ pub async fn edit_message( .get_message(&channel_id, &message_id)? .ok_or_else(|| anyhow::anyhow!("Message not found"))?; - let author_pubkey = crate::signature::to_string(&verifying_key); + let author_pubkey = crate::crypto::to_string(&verifying_key); if existing.author != author_pubkey { anyhow::bail!("Not authorized to edit this message"); } - let server_pubkey_string = crate::signature::to_string(&server.key.verifying_key()); + let server_pubkey_string = crate::crypto::to_string(&server.key.verifying_key()); let signed_string = format!( "{}@{}@{}", existing.data.timestamp, server_pubkey_string, new_content ); - let signature = crate::signature::from_string_sig(&new_signature) + let signature = crate::crypto::from_string_sig(&new_signature) .map_err(|_| anyhow::anyhow!("Invalid signature encoding"))?; verifying_key @@ -146,7 +146,7 @@ pub async fn delete_message( .get_message(&channel_id, &message_id)? .ok_or_else(|| anyhow::anyhow!("Message not found"))?; - let author_pubkey = crate::signature::to_string(&verifying_key); + let author_pubkey = crate::crypto::to_string(&verifying_key); if existing.author != author_pubkey { anyhow::bail!("Not authorized to delete this message"); } diff --git a/src/server.rs b/src/server.rs index c6ca1f3..2f20ec4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -35,7 +35,7 @@ pub struct Server { impl Server { pub async fn new() -> anyhow::Result> { Ok(Arc::new(Self { - key: crate::signature::get().await?, + key: crate::crypto::get().await?, config: Config::get().await?, clients: Mutex::new(HashMap::new()), message_store: MessageStore::new(PathBuf::from("messages"))?, @@ -53,7 +53,7 @@ impl Server { Ok((client, public_key, meta)) => { if let Err(e) = s .user_store - .upsert_user(&crate::signature::to_string(&public_key), &meta) + .upsert_user(&crate::crypto::to_string(&public_key), &meta) .await { eprintln!("Failed to upsert client: {e}");