https://github.com/zanaptak/bufferedcryptorandom
A buffered and thread-safe wrapper around the cryptographic random number generator for .NET and Fable. Compatible with the System.Random interface, plus additional convenience methods.
https://github.com/zanaptak/bufferedcryptorandom
random rng
Last synced: 20 days ago
JSON representation
A buffered and thread-safe wrapper around the cryptographic random number generator for .NET and Fable. Compatible with the System.Random interface, plus additional convenience methods.
- Host: GitHub
- URL: https://github.com/zanaptak/bufferedcryptorandom
- Owner: zanaptak
- License: mit
- Created: 2019-08-28T04:26:26.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-12T20:29:52.000Z (over 3 years ago)
- Last Synced: 2025-04-08T10:47:39.725Z (22 days ago)
- Topics: random, rng
- Language: F#
- Homepage:
- Size: 44.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Zanaptak.BufferedCryptoRandom
[](https://github.com/zanaptak/BufferedCryptoRandom) [](https://www.nuget.org/packages/Zanaptak.BufferedCryptoRandom)
A buffered and thread-safe wrapper around the cryptographic random number generator for [.NET](https://dotnet.microsoft.com/) and [Fable](https://fable.io/). Compatible with the System.Random interface, plus additional convenience methods.
Because the cryptographic provider can be slower than the standard RNG, this library improves performance by using an internal buffer to serve multiple requests, only refilling from the cryptographic provider when necessary.
## Usage
Add the [NuGet package](https://www.nuget.org/packages/Zanaptak.BufferedCryptoRandom) to your project:
```
dotnet add package Zanaptak.BufferedCryptoRandom
```### C#
```cs
using Zanaptak.BufferedCryptoRandom;
var random = new BufferedCryptoRandom();
var randomValue = random.Next();
var diceRoll = random.Next(1, 7);
var coinFlip = random.NextBoolean();
Console.WriteLine($"{randomValue}, {diceRoll}, {coinFlip}");
```### F#
```fs
open Zanaptak.BufferedCryptoRandom
let random = BufferedCryptoRandom()
let randomValue = random.Next()
let diceRoll = random.Next(1, 7)
let coinFlip = random.NextBoolean()
printfn $"{randomValue}, {diceRoll}, {coinFlip}"
```## Cryptographic provider
- For .NET, [`Cryptography.RandomNumberGenerator`](https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator?view=netstandard-2.0) is used.
- For Fable, [`window.crypto.getRandomValues`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) is used if available in the runtime environment. If not, then by default an exception is thrown, but the `fableAllowNonCrypto` option can be used to fall back to [`Math.random`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) in that case.
- When compiling a Node.js app with Fable, the `ZANAPTAK_NODEJS_CRYPTO` compilation symbol can be defined to enable use of [`crypto.randomFillSync`](https://nodejs.org/api/crypto.html#crypto_crypto_randomfillsync_buffer_offset_size) (e.g. for Fable 3: `dotnet fable --define ZANAPTAK_NODEJS_CRYPTO`). This mechanism is used to hide the Node.js import during the default compilation to avoid interfering with web bundlers while still allowing the same codebase to work in both environments.## Thread-safety
By default, the library uses exclusive locking when accessing the internal buffer for thread safety. The `threadSafe` option can be set to false to disable this for additional performance at the expense of safety.
## API
See the [API documentation](https://github.com/zanaptak/BufferedCryptoRandom/blob/main/doc/api.md).
## Benchmarks
See the [benchmark project](https://github.com/zanaptak/BufferedCryptoRandom/tree/main/benchmark).