Files
Reims-Go/patches/reims-ctl-win32-random-bytes.patch
T

31 lines
1.4 KiB
Diff

diff --git a/crates/reims-ctl/src/vm.rs b/crates/reims-ctl/src/vm.rs
index e8b8e92..f62174f 100644
--- a/crates/reims-ctl/src/vm.rs
+++ b/crates/reims-ctl/src/vm.rs
@@ -853,12 +853,22 @@ fn random_bytes<const N: usize>() -> [u8; N] {
.and_then(|mut f| f.read_exact(&mut b))
.is_err()
{
+ // Windows has no /dev/urandom. The clock alone is no random source there: it counts
+ // 100 ns, so its low bytes hold some 22 bits, the first of them a multiple of 4, and the
+ // pid, shifted past them, changes none. std's RandomState is keyed from the OS's random
+ // source (ProcessPrng on Windows), so a hash it keys is unpredictable; the clock and the
+ // pid are hashed in too.
+ use std::hash::{BuildHasher, Hasher};
let t = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
- let seed = t.as_nanos() ^ (std::process::id() as u128) << 64;
- for (i, x) in b.iter_mut().enumerate() {
- *x = (seed >> (i * 8)) as u8;
+ let keys = std::collections::hash_map::RandomState::new();
+ for (i, chunk) in b.chunks_mut(8).enumerate() {
+ let mut h = keys.build_hasher();
+ h.write_usize(i);
+ h.write_u128(t.as_nanos());
+ h.write_u32(std::process::id());
+ chunk.copy_from_slice(&h.finish().to_le_bytes()[..chunk.len()]);
}
}
b