From 7ca1b5aeacd9633e5a3dca9f7596975dbc686508 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 1 Mar 2026 14:39:56 +0100 Subject: slowly preparing for actual casino --- src/bin/client/database.rs | 54 ++++++++-------------------------------------- 1 file changed, 9 insertions(+), 45 deletions(-) (limited to 'src/bin/client/database.rs') diff --git a/src/bin/client/database.rs b/src/bin/client/database.rs index 03a6c45..da069a7 100644 --- a/src/bin/client/database.rs +++ b/src/bin/client/database.rs @@ -1,10 +1,4 @@ use rusqlite::Connection; -use aes_gcm::{Aes256Gcm, Key, Nonce}; -use aes_gcm::aead::{Aead, KeyInit}; -use pbkdf2::pbkdf2; -use hmac::Hmac; -use sha2::Sha256; -use rand::Rng; pub fn init_db() -> Connection { let conn = Connection::open("client.db").unwrap(); @@ -19,7 +13,7 @@ pub fn init_db() -> Connection { "CREATE TABLE IF NOT EXISTS credentials ( host TEXT PRIMARY KEY, username TEXT NOT NULL, - encrypted_password BLOB NOT NULL + password TEXT NOT NULL )", [], ).unwrap(); @@ -43,52 +37,22 @@ pub fn save_host(conn: &Connection, host: &str) { ).unwrap(); } -pub fn save_credentials(conn: &Connection, host: &str, username: &str, password: &str) -> Result<(), aes_gcm::Error> { - let encrypted = encrypt_password(password)?; +pub fn save_credentials(conn: &Connection, host: &str, username: &str, password: &str) -> Result<(), Box> { conn.execute( - "INSERT OR REPLACE INTO credentials (host, username, encrypted_password) VALUES (?1, ?2, ?3)", - (host, username, &encrypted), - ).unwrap(); + "INSERT OR REPLACE INTO credentials (host, username, password) VALUES (?1, ?2, ?3)", + (host, username, password), + )?; Ok(()) } pub fn get_credentials(conn: &Connection, host: &str) -> Option<(String, String)> { - let mut stmt = conn.prepare("SELECT username, encrypted_password FROM credentials WHERE host = ?1").unwrap(); + let mut stmt = conn.prepare("SELECT username, password FROM credentials WHERE host = ?1").unwrap(); let mut rows = stmt.query([host]).unwrap(); if let Ok(Some(row)) = rows.next() { let username: String = row.get(0).unwrap(); - let encrypted: Vec = row.get(1).unwrap(); - decrypt_password(&encrypted).map(|password| (username, password)) + let password: String = row.get(1).unwrap(); + Some((username, password)) } else { None } -} - -fn derive_key() -> Key { - let mut key = [0u8; 32]; - let salt = b"client_salt"; // Fixed salt for simplicity - pbkdf2::>(b"client_key", salt, 10000, &mut key).expect("error deriving key"); - key.into() -} - -fn encrypt_password(password: &str) -> Result, aes_gcm::Error> { - let key = derive_key(); - let cipher = Aes256Gcm::new(&key); - let mut nonce_bytes = [0u8; 12]; - rand::thread_rng().fill(&mut nonce_bytes); - let nonce = Nonce::from_slice(&nonce_bytes); - let ciphertext = cipher.encrypt(nonce, password.as_bytes())?; - let mut result = nonce_bytes.to_vec(); - result.extend(ciphertext); - Ok(result) -} - -fn decrypt_password(encrypted: &[u8]) -> Option { - if encrypted.len() < 12 { - return None; - } - let key = derive_key(); - let cipher = Aes256Gcm::new(&key); - let nonce = Nonce::from_slice(&encrypted[..12]); - cipher.decrypt(nonce, &encrypted[12..]).ok().and_then(|plaintext| String::from_utf8(plaintext).ok()) -} +} \ No newline at end of file -- cgit v1.2.3