ddn.os.entropy
Cryptographically secure random bytes from the operating system.
fillEntropy fills caller-owned storage without allocating or throwing. It uses getrandom on Linux, arc4random_buf on FreeBSD and BCryptGenRandom on Windows. There is no application-level PRNG, seed, byte cache or shared mutable state. Independent callers may use it concurrently; the operating system owns reseeding and fork handling.
Check the returned EntropyStatus before using the bytes. An empty buffer succeeds without consulting the OS, even on unsupported platforms. Nonempty requests on other platforms, including macOS for now, fail explicitly. Linux waits for the kernel random source to be initialized; a missing or denied getrandom is an error, not a weaker-source fallback.
Example:
import ddn.os.entropy;
ubyte[32] bytes;
auto result = fillEntropy(bytes[]);
if (result) {
// All 32 bytes are ready to use.
} else {
auto failure = result.error(); // classification and native error code
// Report the failure; do not use bytes.
}Design record: ADR-2026-11-26-02.
Types 2
An OS entropy failure, suitable for allocation-free diagnostics.
Code codeWhich failure occurred.int nativeCodeLinux `errno` or Windows `NTSTATUS` for `OS_FAILURE`; zero otherwise. Windows status codes retain their signed 32-bit representation.CodePortable failure classification.Success without a payload, or an EntropyError.
Functions 1
EntropyStatus fillEntropy(scope ubyte[] buffer) @trusted nothrow @nogcFills the entire buffer with cryptographically secure random bytes.