intro.bbdoc 2.2 KB

123456789101112131415161718192021222324252627282930313233
  1. The Random.Secure module provides a robust and secure way to generate random numbers across each platform.
  2. This module is designed to leverage the underlying cryptographic services of each operating system,
  3. ensuring that the random numbers produced are high-quality.
  4. Because of this, this particular module does not provide a way to seed the random number generator.
  5. ## Windows
  6. On Windows, Random.Secure uses the Cryptographic Application Programming Interface (CryptoAPI).
  7. Specifically, it employs the `CryptGenRandom` function, which is part of the Windows Cryptography API: Next Generation (CNG).
  8. This function accesses the system's cryptographic service provider to produce a stream of random bytes.
  9. These bytes are considered cryptographically secure, as they are generated using a random number generator (RNG) that has been seeded
  10. with the current system time, process ID, thread ID, and various other high-entropy sources. The use of `CryptGenRandom` ensures that the
  11. randomness is of high quality and the API takes care of all the complex entropy management and seed generation.
  12. ## macOS
  13. For macOS, the Random.Secure module taps into the native Security framework, utilizing the `SecRandomCopyBytes` function.
  14. This function is a part of Apple's security best practices and provides an interface to a cryptographically secure random number generator.
  15. Unlike reading from /dev/random or /dev/urandom, which can be less efficient and direct, `SecRandomCopyBytes` ensures that the random data is
  16. retrieved efficiently and is of high quality. It automatically handles entropy pooling and reseeding, pulling from the same entropy sources
  17. that the OS itself uses for cryptographic operations.
  18. ## Linux
  19. On Linux systems, Random.Secure reads directly from the `/dev/urandom` device file.
  20. Unlike `/dev/random`, `/dev/urandom` does not block if there is perceived to be a lack of entropy; instead, it reuses the internal pool
  21. to produce more pseudo-random bits. This makes `/dev/urandom` suitable for high-throughput random number generation without the risk of
  22. stalling the application.
  23. The module manages the file descriptor for `/dev/urandom` efficiently, opening it once and reading from it as needed,
  24. which avoids the overhead of repeatedly opening and closing the file.