embedded_websocket/
random.rs

1use rand_core::RngCore;
2
3// Clients need a proper random number generator to generate a mask key
4// However, server websockets do not require this and the payload is not masked
5// EmptyRng is used for servers. In theory, you can use it for clients but you may run into
6// proxy server caching issues so it is advisable to use a proper random number generator.
7// Note that data masking does not require a cryptographically strong random number because
8// the key is sent with the payload anyway
9
10#[derive(Default)]
11pub struct EmptyRng {}
12
13impl EmptyRng {
14    pub fn new() -> EmptyRng {
15        EmptyRng {}
16    }
17}
18
19impl RngCore for EmptyRng {
20    fn next_u32(&mut self) -> u32 {
21        0
22    }
23    fn next_u64(&mut self) -> u64 {
24        0
25    }
26    fn fill_bytes(&mut self, _dest: &mut [u8]) {}
27    fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> core::result::Result<(), rand_core::Error> {
28        Ok(())
29    }
30}