Skip to content

Commit

Permalink
Fixes and still debugging collision regression
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Mar 29, 2024
1 parent 2fcde83 commit 317ec2a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 31 deletions.
15 changes: 14 additions & 1 deletion lib/rng.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
28 changes: 14 additions & 14 deletions lib/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -233,9 +233,9 @@ class Uuid {
{@Deprecated('use config instead. Removal in 5.0.0')
Map<String, dynamic>? 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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -373,9 +373,9 @@ class Uuid {
{@Deprecated('use config instead. Removal in 5.0.0')
Map<String, dynamic>? 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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -471,9 +471,9 @@ class Uuid {
{@Deprecated('use config instead. Removal in 5.0.0')
Map<String, dynamic>? 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
Expand Down
3 changes: 2 additions & 1 deletion lib/v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> nodeId = [
Expand Down
4 changes: 3 additions & 1 deletion lib/v4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> rng = options?.rng?.generate() ??
goptions?.rng?.generate() ??
MathRNG().generate();
options?.rng?.generate() ??
MathRNG.noSeed().generate();

// Use provided values over RNG
List<int> rnds = options?.random ?? rng;
Expand Down
3 changes: 2 additions & 1 deletion lib/v6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> nodeId = [
Expand Down
2 changes: 1 addition & 1 deletion lib/v7.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UuidV7 {

buf.setAll(0, timeList48);
List<int> 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]);
Expand Down
2 changes: 1 addition & 1 deletion lib/v8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]', () {
Expand Down Expand Up @@ -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));
Expand All @@ -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';
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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]));
Expand Down

0 comments on commit 317ec2a

Please sign in to comment.