ChaChaRandom class
ChaCha20-based SecureRandom implementation.
The throughput is up to about 0.25 GB of random data per second. This is over 100 times faster than Random.secure on many platforms.
Current algorithm
The default behavior is:
- 12 rounds of ChaCha. No key extraction attack has been proposed against ChaCha with more than 6 rounds so the choice has a good margin of safety.
- 256-bit secret key is mixed (XOR) with numbers from Random.secure at least once every 8192 blocks. This is also done if more than 10 milliseconds has passed since the last reseed event.
- After a block has been computed, the block counter is incremented.
- After a block has been computed, the last 128 bits of the secret key is immediately mixed (XOR) with the first 128 bits of the state. The first 128-bits of the state are then zeroed and skipped. This provides backtracking resistance.
- State bits are erased after they have been read so a memory dump won't reveal them.
Example
import 'package:cryptography/random.dart';
void main() {
final random = SecureRandom.fast;
final x = random.nextInt(100);
print('x = $x');
}
- Inheritance
-
- Object
- SecureRandom
- ChaChaRandom
Constructors
- ChaChaRandom({int rounds = defaultRounds, int maxBlocksBeforeReseed = defaultMaxBlocksBeforeReseed, Duration? maxDurationBeforeReseed = defaultMaxDurationBeforeReseed})
- ChaChaRandom.forTesting({int seed = 0})
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isSecure → bool
-
Tells whether the algorithm is cryptographically secure and
the initial entropy is from Random.secure.
no setteroverride
- maxBlocksBeforeReseed → int
-
Maximum number of 64 byte blocks since the last reseed before the random
number generator must be reseeded.
final
- maxDurationBeforeReseed → Duration?
-
Maximum elapsed time since the last reseed before the random number
generator must be reseeded.
final
- reseedCount → int
-
no setter
- rounds → int
-
Number of ChaCha rounds.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
nextBool(
) → bool -
Generates a random boolean value.
inherited
-
nextDouble(
) → double -
Generates a non-negative random floating point value uniformly distributed
in the range from 0.0, inclusive, to 1.0, exclusive.
inherited
-
nextInt(
int max) → int -
Generates a non-negative random integer uniformly distributed in the range
from 0, inclusive, to
max, exclusive.inherited -
nextUint32(
) → int -
Returns a random unsigned 32-bit integer.
override
-
nextUint52(
[int? max]) → int -
Returns a random cross-platform unsigned 52-bit integer.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void -
override
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- defaultMaxBlocksBeforeReseed → const int
- Default value for maxBlocksBeforeReseed.
- defaultMaxDurationBeforeReseed → const Duration
- Default value for maxDurationBeforeReseed.
- defaultRounds → const int
- Default value for rounds.