From 15d3ab79bd5b27fe540015dd790d892142d5dab4 Mon Sep 17 00:00:00 2001 From: code002lover Date: Mon, 12 Jan 2026 23:58:55 +0100 Subject: [PATCH] use proper rand --- Cargo.lock | 1 + backend/Cargo.toml | 1 + backend/src/auth_persistence.rs | 36 +-------------------------------- 3 files changed, 3 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b738204..7063a38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,6 +147,7 @@ dependencies = [ "prost", "prost-build", "prost-types", + "rand 0.9.2", "reqwest", "rocket", "rocket_prost_responder_derive", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 29eef94..f130578 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -19,6 +19,7 @@ serde_json = "1.0" reqwest = { version = "0.13", features = ["json"] } aes-gcm = "0.10" base64 = "0.22" +rand = "0.9.2" [build-dependencies] prost-build = "0.14" diff --git a/backend/src/auth_persistence.rs b/backend/src/auth_persistence.rs index f367ecd..68748a3 100644 --- a/backend/src/auth_persistence.rs +++ b/backend/src/auth_persistence.rs @@ -185,7 +185,7 @@ impl AuthStorage { } }; - let nonce_bytes: [u8; NONCE_SIZE] = rand::thread_rng().generate_bytes(); + let nonce_bytes: [u8; NONCE_SIZE] = rand::random(); let nonce = Nonce::from_slice(&nonce_bytes); let ciphertext = match self.cipher.encrypt(nonce, plaintext.as_ref()) { Ok(encrypted) => encrypted, @@ -225,37 +225,3 @@ impl AuthStorage { } } } - -mod rand { - use std::cell::Cell; - - thread_local! { - static STATE: Cell = Cell::new( - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_nanos() as u64 - ); - } - - pub struct ThreadRng; - - pub fn thread_rng() -> ThreadRng { - ThreadRng - } - - impl ThreadRng { - pub fn generate_bytes(&mut self) -> [u8; N] { - let mut result = [0u8; N]; - STATE.with(|state| { - let mut s = state.get(); - for byte in result.iter_mut() { - s = s.wrapping_mul(1103515245).wrapping_add(12345); - *byte = (s >> (s % 8)) as u8; - } - state.set(s); - }); - result - } - } -}