Skip to content

Commit

Permalink
#59 Remove the need for shuffle in UuidUtil.mathRNG
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 authored and daegalus committed Mar 1, 2021
1 parent da17534 commit e367a85
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions lib/uuid_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@
library uuid_util;

import 'dart:math';

import 'dart:typed_data';

class UuidUtil {
/// Math.Random()-based RNG. All platforms, fast, not cryptographically strong. Optional Seed passable.
static Uint8List mathRNG({int seed = -1}) {
var b = Uint8List(16);
static final _random = Random();
static final _secureRandom = Random.secure();

var rand = (seed == -1) ? Random() : Random(seed);
/// Math.Random()-based RNG. All platforms, fast, not cryptographically
/// strong. Optional Seed passable.
static Uint8List mathRNG({int seed = -1}) {
final b = Uint8List(16);
final rand = (seed == -1) ? _random : Random(seed);

for (var i = 0; i < 16; i++) {
b[i] = rand.nextInt(256);
}

(seed == -1) ? b.shuffle() : b.shuffle(Random(seed));

return b;
}

/// Crypto-Strong RNG. All platforms, unknown speed, cryptographically strong (theoretically)
/// Crypto-Strong RNG. All platforms, unknown speed, cryptographically strong
/// (theoretically)
static Uint8List cryptoRNG() {
var b = Uint8List(16);
var rand = Random.secure();
final b = Uint8List(16);

for (var i = 0; i < 16; i++) {
b[i] = rand.nextInt(256);
b[i] = _secureRandom.nextInt(256);
}

return b;
}
}
4 changes: 2 additions & 2 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void main() {
'rng': UuidUtil.mathRNG,
'namedArgs': Map.fromIterables([const Symbol('seed')], [1])
});
var u1 = '4dff2ea7-7bc8-4fea-8da0-a4993073bdb3';
var u1 = 'a473ff7b-b3cd-4899-a04d-ea0fbd30a72e';
expect(u0, equals(u1));
});

Expand All @@ -129,7 +129,7 @@ void main() {
'namedArgs': Map.fromIterables([const Symbol('seed')], [1])
});

var u1 = '4dff2ea7-7bc8-4fea-8da0-a4993073bdb3';
var u1 = 'a473ff7b-b3cd-4899-a04d-ea0fbd30a72e';
expect(Uuid.unparse(buffer), equals(u1));
});

Expand Down

0 comments on commit e367a85

Please sign in to comment.