diff --git a/CHANGELOG.md b/CHANGELOG.md index 4744328..2fd1218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rng.dart b/lib/rng.dart index f008fb8..0307cfd 100644 --- a/lib/rng.dart +++ b/lib/rng.dart @@ -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); diff --git a/pubspec.yaml b/pubspec.yaml index d858276..a3c6d41 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/uuid_test.dart b/test/uuid_test.dart index cbda671..1c8b9c1 100644 --- a/test/uuid_test.dart +++ b/test/uuid_test.dart @@ -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)); }); @@ -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)); }); @@ -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.', () { @@ -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, () {