Skip to content

Commit

Permalink
Change MathRNG internal loop, and update SDK constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Dec 12, 2023
1 parent de13354 commit 974b459
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

v4.3.0

* Update SDK constraints to >= 3.0.0
* Update Meta package to 1.11.0
* **[PARTIAL BREAKING CHANGE]** Changing MathRNG implementation to use 4 nextints, instead of 16 for optimization to match CryptoRNG. This will affect regenerating the same UUID from the same seed.
* If you need the old behavior, please use MathRNGDeprecated() instead.

v4.2.2

* Fix CryptoRNG on Web generating a random number 0 always
Expand Down
25 changes: 25 additions & 0 deletions lib/rng.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ class MathRNG extends RNG {

const MathRNG({this.seed = -1});

@override
Uint8List generateInternal() {
final b = Uint8List(16);
final rand = (seed == -1) ? _random : Random(seed);

for (var i = 0; i < 16; i += 4) {
var k = rand.nextInt(pow(2, 32).toInt());
b[i] = k;
b[i + 1] = k >> 8;
b[i + 2] = k >> 16;
b[i + 3] = k >> 24;
}

return b;
}
}

/// Math.Random()-based RNG. All platforms, fast, not cryptographically
/// strong. Optional [seed] can be passed on creation.
class MathRNGDeprecated extends RNG {
static final _random = Random();
final int seed;

const MathRNGDeprecated({this.seed = -1});

@override
Uint8List generateInternal() {
final b = Uint8List(16);
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: uuid
version: 4.2.2
version: 4.3.0
description: >
RFC4122 (v1, v4, v5, v6, v7, v8) UUID Generator and Parser for Dart
documentation: https://daegalus.github.io/dart-uuid/index.html
repository: https://github.com/Daegalus/dart-uuid
environment:
sdk: ">=2.14.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
dependencies:
crypto: ^3.0.0
sprintf: ^7.0.0
meta: ^1.9.1
meta: ^1.11.0
dev_dependencies:
lints: ^3.0.0
test: ^1.24.9
Expand Down
8 changes: 4 additions & 4 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void main() {
var u0 = uuid.v4(options: {
'rng': MathRNG(seed: 1),
});
var u1 = 'a473ff7b-b3cd-4899-a04d-ea0fbd30a72e';
var u1 = 'a462502a-73af-4e41-bfc4-05957b7030dd';
expect(u0, equals(u1));
});

Expand All @@ -130,7 +130,7 @@ void main() {
'rng': MathRNG(seed: 1),
});

var u1 = 'a473ff7b-b3cd-4899-a04d-ea0fbd30a72e';
var u1 = 'a462502a-73af-4e41-bfc4-05957b7030dd';
expect(Uuid.unparse(buffer), equals(u1));
});

Expand Down Expand Up @@ -363,7 +363,7 @@ void main() {
var options = V7Options(1321651533573, rand);
var id = uuid.v7(config: options);

expect(id, equals('0133b891-f705-7473-bf7b-b3cdc899a04d'));
expect(id, equals('0133b891-f705-7462-902a-73af3e41ffc4'));
});

test('Generate lots of codes to see if we get v7 collisions.', () {
Expand Down Expand Up @@ -575,7 +575,7 @@ void main() {
for (final testCase in {
'Tuesday, February 22, 2022 2:22:22.222000 PM GMT-05:00': [
DateTime.fromMillisecondsSinceEpoch(1645557742222).toUtc(),
'20220222-1922-8422-B222-B3CDC899A04D'
'20220222-1922-8422-9222-73AF3E41FFC4'
],
}.entries) {
test(testCase.key, () {
Expand Down

0 comments on commit 974b459

Please sign in to comment.