diff --git a/lib/rng.dart b/lib/rng.dart index 260bf57..f9c0ec6 100644 --- a/lib/rng.dart +++ b/lib/rng.dart @@ -27,7 +27,20 @@ abstract class RNG { class MathRNG extends RNG { final Random _rnd; - MathRNG({int? seed}) : _rnd = Random(seed); + factory MathRNG.fromSeed({int? seed}) { + Random rnd = seed == null ? Random() : Random(seed); + return MathRNG._internal(rnd); + } + + factory MathRNG.fromRandom({Random? rnd}) { + return MathRNG._internal(rnd ?? Random()); + } + + factory MathRNG.noSeed() { + return MathRNG._internal(Random()); + } + + const MathRNG._internal(this._rnd); @override Uint8List generateInternal() { diff --git a/lib/uuid.dart b/lib/uuid.dart index e65f7be..0e531eb 100644 --- a/lib/uuid.dart +++ b/lib/uuid.dart @@ -200,7 +200,7 @@ class Uuid { V1Options? config, int offset = 0, }) { - var result = config != null ? v1(config: config) : v1(options: options); + var result = options != null ? v1(options: options) : v1(config: config); return UuidParsing.parse(result, buffer: buffer, offset: offset); } @@ -233,9 +233,9 @@ class Uuid { {@Deprecated('use config instead. Removal in 5.0.0') Map? options, V1Options? config}) { - return config != null - ? UuidValue.fromString(v1(config: config)) - : UuidValue.fromString(v1(options: options)); + return options != null + ? UuidValue.fromString(v1(options: options)) + : UuidValue.fromString(v1(config: config)); } /// Generates a RNG version 4 UUID @@ -339,7 +339,7 @@ class Uuid { V4Options? config, int offset = 0, }) { - var result = config != null ? v4(config: config) : v4(options: options); + var result = options != null ? v4(options: options) : v4(config: config); return UuidParsing.parse(result, buffer: buffer, offset: offset); } @@ -373,9 +373,9 @@ class Uuid { {@Deprecated('use config instead. Removal in 5.0.0') Map? options, V4Options? config}) { - return config != null - ? UuidValue.fromString(v4(config: config)) - : UuidValue.fromString(v4(options: options)); + return options != null + ? UuidValue.fromString(v4(options: options)) + : UuidValue.fromString(v4(config: config)); } /// Generates a namespace & name-based version 5 UUID @@ -440,9 +440,9 @@ class Uuid { V5Options? config, int offset = 0, }) { - var result = config != null - ? v5(namespace, name, config: config) - : v5(namespace, name, options: options); + var result = options != null + ? v5(namespace, name, options: options) + : v5(namespace, name, config: config); return UuidParsing.parse(result, buffer: buffer, offset: offset); } @@ -471,9 +471,9 @@ class Uuid { {@Deprecated('use config instead. Removal in 5.0.0') Map? options, V5Options? config}) { - return config != null - ? UuidValue.fromString(v5(namespace, name, config: config)) - : UuidValue.fromString(v5(namespace, name, options: options)); + return options != null + ? UuidValue.fromString(v5(namespace, name, options: options)) + : UuidValue.fromString(v5(namespace, name, config: config)); } /// Generates a draft time-based version 6 UUID diff --git a/lib/v1.dart b/lib/v1.dart index 1cdf0a0..7e2331d 100644 --- a/lib/v1.dart +++ b/lib/v1.dart @@ -12,7 +12,8 @@ class UuidV1 { /// Primarily sets up the seedBytes then generates the node id and clockseq void _init() { if (V1State.initialized) return; - Uint8List seedBytes = goptions?.rng?.generate() ?? MathRNG().generate(); + Uint8List seedBytes = + goptions?.rng?.generate() ?? MathRNG.noSeed().generate(); // Per 4.5, create a 48-bit node id (47 random bits + multicast bit = 1) List nodeId = [ diff --git a/lib/v4.dart b/lib/v4.dart index 522b755..dec3821 100644 --- a/lib/v4.dart +++ b/lib/v4.dart @@ -18,9 +18,11 @@ class UuidV4 { /// http://tools.ietf.org/html/rfc4122.html#section-4.4 String generate({V4Options? options}) { // Use the built-in RNG or a custom provided RNG + List rng = options?.rng?.generate() ?? goptions?.rng?.generate() ?? - MathRNG().generate(); + options?.rng?.generate() ?? + MathRNG.noSeed().generate(); // Use provided values over RNG List rnds = options?.random ?? rng; diff --git a/lib/v6.dart b/lib/v6.dart index 5d83aab..c075744 100644 --- a/lib/v6.dart +++ b/lib/v6.dart @@ -16,7 +16,8 @@ class UuidV6 { /// Primarily sets up the seedBytes then generates the node id and clockseq void _init() { if (V6State.initialized) return; - Uint8List seedBytes = goptions?.rng?.generate() ?? MathRNG().generate(); + Uint8List seedBytes = + goptions?.rng?.generate() ?? MathRNG.noSeed().generate(); // Per 4.5, create a 48-bit node id (47 random bits + multicast bit = 1) List nodeId = [ diff --git a/lib/v7.dart b/lib/v7.dart index a3fe126..d7ee034 100644 --- a/lib/v7.dart +++ b/lib/v7.dart @@ -34,7 +34,7 @@ class UuidV7 { buf.setAll(0, timeList48); List randomBytes = options?.randomBytes ?? - (goptions?.rng?.generate() ?? MathRNG().generate()); + (goptions?.rng?.generate() ?? MathRNG.noSeed().generate()); buf.setRange(6, 16, randomBytes); buf.setRange(6, 7, [buf.getRange(6, 7).last & 0x0f | 0x70]); diff --git a/lib/v8.dart b/lib/v8.dart index fe986e9..048548c 100644 --- a/lib/v8.dart +++ b/lib/v8.dart @@ -56,7 +56,7 @@ class UuidV8 { 5, 6, UuidParsing.parseHexToBytes(sprintf('0x%02i', [time.minute]))); var randomBytes = options?.randomBytes ?? - (goptions?.rng?.generate() ?? MathRNG().generate()); + (goptions?.rng?.generate() ?? MathRNG.noSeed().generate()); buf.setRange(6, 16, randomBytes); buf.setRange(6, 7, [buf.getRange(6, 7).last & 0x0f | 0x80]); diff --git a/pubspec.yaml b/pubspec.yaml index 20f6502..afac1a0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,7 +7,7 @@ repository: https://github.com/Daegalus/dart-uuid environment: sdk: ">=3.0.0 <4.0.0" dependencies: - crypto: ^3.0.0 + cryptography: ^2.7.0 sprintf: ^7.0.0 meta: ^1.10.0 fixnum: ^1.1.0 diff --git a/test/uuid_test.dart b/test/uuid_test.dart index fecdfe5..935a4ef 100644 --- a/test/uuid_test.dart +++ b/test/uuid_test.dart @@ -8,7 +8,7 @@ import 'package:uuid/uuid.dart'; import 'package:uuid/rng.dart'; void main() { - var uuid = const Uuid(); + var uuid = Uuid(); const testTime = 1321644961388; group('[Version 1 Tests]', () { @@ -118,7 +118,7 @@ void main() { group('[Version 4 Tests]', () { test('Check if V4 is consistent using a static seed', () { var u0 = uuid.v4(options: { - 'rng': MathRNG(seed: 1), + 'rng': MathRNG.fromSeed(seed: 1), }); var u1 = 'a462502a-73af-4e41-bfc4-05957b7030dd'; expect(u0, equals(u1)); @@ -127,7 +127,7 @@ void main() { test('Consistency check with buffer', () { var buffer = Uint8List(16); uuid.v4buffer(buffer, options: { - 'rng': MathRNG(seed: 1), + 'rng': MathRNG.fromSeed(seed: 1), }); var u1 = 'a462502a-73af-4e41-bfc4-05957b7030dd'; @@ -136,10 +136,10 @@ void main() { test('Using Objects', () { var regular = uuid.v4(options: { - 'rng': MathRNG(seed: 1), + 'rng': MathRNG.fromSeed(seed: 1), }); var obj = uuid.v4obj(options: { - 'rng': MathRNG(seed: 1), + 'rng': MathRNG.fromSeed(seed: 1), }); expect(obj.uuid, equals(regular)); @@ -185,7 +185,7 @@ void main() { var numDuplicates = 0; for (var i = 0; i < numToGenerate; i++) { - final uuid = generator.v4(); + final uuid = generator.v4(config: V4Options(null, MathRNG.noSeed())); if (!values.contains(uuid)) { values.add(uuid); @@ -359,7 +359,7 @@ void main() { }); test('Explicit options produce expected id', () { - final rand = MathRNG(seed: 1).generate(); + final rand = MathRNG.fromSeed(seed: 1).generate(); var options = V7Options(1321651533573, rand); var id = uuid.v7(config: options); @@ -395,7 +395,7 @@ void main() { test('Using buffers', () { var buffer = Uint8List(16); - final rand = MathRNG(seed: 1).generate(); + final rand = MathRNG.fromSeed(seed: 1).generate(); var options = V7Options(testTime, rand); var wihoutBuffer = uuid.v7(config: options); @@ -405,7 +405,7 @@ void main() { }); test('Using Objects', () { - final rand = MathRNG(seed: 1).generate(); + final rand = MathRNG.fromSeed(seed: 1).generate(); var options = V7Options(testTime, rand); var regular = uuid.v7(config: options); @@ -601,7 +601,7 @@ void main() { ], }.entries) { test(testCase.key, () { - final rand = MathRNG(seed: 1).generate(); + final rand = MathRNG.fromSeed(seed: 1).generate(); final uuid = Uuid().v8(config: V8Options(testCase.value[0] as DateTime, rand)); expect(uuid.toUpperCase(), equals(testCase.value[1]));