use proper rand

This commit is contained in:
code002lover 2026-01-12 23:58:55 +01:00
parent 4abe8a53df
commit 15d3ab79bd
3 changed files with 3 additions and 35 deletions

1
Cargo.lock generated
View File

@ -147,6 +147,7 @@ dependencies = [
"prost",
"prost-build",
"prost-types",
"rand 0.9.2",
"reqwest",
"rocket",
"rocket_prost_responder_derive",

View File

@ -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"

View File

@ -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<u64> = 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<const N: usize>(&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
}
}
}