torrentdht 0.1.1

DHT discovery with mTLS handshake handoff; optional BitTorrent when the host opts in
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! CSPRNG helpers for protocol nonces and transaction IDs.
//!
//! Do not use xorshift/`thread_local` PRNGs for anything that authenticates,
//! correlates, or binds network messages — those are forgeable/predictable.

use crate::error::{Error, Result};

/// Fill `out` from the OS CSPRNG.
pub fn fill_random(out: &mut [u8]) -> Result<()> {
    getrandom::fill(out).map_err(|_| Error::Config("CSPRNG failure".into()))
}

/// Cryptographically random `N`-byte transaction / correlation id.
pub fn random_bytes<const N: usize>() -> Result<[u8; N]> {
    let mut out = [0u8; N];
    fill_random(&mut out)?;
    Ok(out)
}