Operating System Random APIs
What you'll learn: How to access cryptographically secure randomness through your operating system's built-in interfaces.
Now that you understand CSPRNGs, you need a practical way to get secure random bytes in real programs. Every major operating system provides APIs specifically designed for cryptographic randomness—these are your go-to tools.
Why OS-Level Random APIs Matter
Operating systems gather entropy from hardware sources (keyboard timing, disk latency, thermal sensors) and manage it centrally. Instead of each application reinventing the wheel, the OS exposes this randomness through standardized interfaces.
Think of it like a city water system: rather than each household digging its own well, everyone taps into a reliable municipal supply that's professionally maintained.
The Main Players
Linux/Unix:
/dev/urandom— The default choice. Non-blocking, always returns data, backed by a CSPRNG seeded from kernel entropy./dev/random— Historically considered "more secure" but blocks when entropy is low. Modern consensus:/dev/urandomis sufficient.getrandom()— System call introduced in Linux 3.17. Blocks only until initially seeded, then behaves like/dev/urandom. Preferred modern approach.
Windows:
CryptGenRandom()— Legacy API from CryptoAPI, still widely used and secure.BCryptGenRandom()— Modern replacement from CNG (Cryptography Next Generation). Recommended for new code.
Which Should You Use?
On Linux: getrandom() if available, otherwise /dev/urandom.
On Windows: BCryptGenRandom() for new projects, CryptGenRandom() for legacy compatibility.
Never use /dev/random unless you have a specific, well-understood reason—it can cause your application to hang unnecessarily.
Key Takeaway: Modern operating systems provide secure random APIs like getrandom() and BCryptGenRandom() that are properly seeded from kernel entropy—always use these instead of rolling your own randomness or using non-cryptographic sources.