Course contentsShow
← Security
Lesson 301 of 3,0527. Randomness and Key ManagementFree lesson

Platform-Specific CSPRNG APIs

Secure random number APIs across platforms: getrandom(), BCryptGenRandom(), SecRandomCopyBytes(), and crypto.getRandomValues().

Platform-Specific CSPRNG APIs

What you'll learn: How different operating systems and platforms provide their own secure random number generation functions that you should use instead of building your own.

Why Platform-Specific APIs Matter

You've learned that CSPRNGs must meet strict security requirements and handle entropy properly. Rather than implementing these complex systems yourself, modern platforms provide battle-tested APIs that do the heavy lifting. Each operating system has its own function name, but they all tap into the platform's secure entropy pool.

The Main Players

Linux/Unix: getrandom()
This system call directly accesses the kernel's CSPRNG. It blocks if insufficient entropy exists at boot, then returns cryptographically secure random bytes.

Windows: BCryptGenRandom()
Part of Windows' Cryptography API: Next Generation (CNG), this function provides access to the operating system's secure random number generator with proper entropy management.

macOS/iOS: SecRandomCopyBytes()
Apple's Security framework provides this function, which uses /dev/random under the hood but with additional safeguards and error handling.

Web Browsers: crypto.getRandomValues()
JavaScript's Web Crypto API exposes this method, allowing web applications to generate secure random numbers without relying on the notoriously weak Math.random().

The Pattern

All these APIs share common traits: they're non-deterministic, properly seeded from system entropy, and designed to resist state compromise. They abstract away platform differences in entropy collection and CSPRNG algorithms.

Think of these APIs as specialized vending machines: you don't need to know how they source ingredients or manufacture the product—you just press the button and receive quality-assured randomness.

Key Takeaway: Always use your platform's native CSPRNG API (getrandom(), BCryptGenRandom(), SecRandomCopyBytes(), or crypto.getRandomValues()) rather than general-purpose random functions—these are specifically designed and maintained for cryptographic security.