Skip to content

Commit

Permalink
Fix UUIDv1
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Jul 10, 2024
1 parent 194363b commit f9862c7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

v4.4.1

* Fix UUIDv1 to use millisecondsSinceEpoch instead of microsecondsSinceEpoch. Matches UUIDv6 and passes vector tests.

v4.4.0

* Fix MathRNG with Seed being recreated on each use, generating the same list of bytes. (thanks @showband)
Expand Down
2 changes: 1 addition & 1 deletion lib/v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class UuidV1 {
// (1582-10-15 00:00). Time is handled internally as 'msecs' (integer
// milliseconds) and 'nsecs' (100-nanoseconds offset from msecs) since unix
// epoch, 1970-01-01 00:00.
int mSecs = options?.mSecs ?? DateTime.timestamp().microsecondsSinceEpoch;
int mSecs = options?.mSecs ?? DateTime.timestamp().millisecondsSinceEpoch;

// Per 4.2.1.2, use count of uuid's generated during the current clock
// cycle to simulate higher resolution clock
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: uuid
version: 4.4.0
version: 4.4.1
description: >
RFC4122 (v1, v4, v5, v6, v7, v8) UUID Generator and Parser for Dart
documentation: https://daegalus.github.io/dart-uuid/index.html
Expand All @@ -9,11 +9,11 @@ environment:
dependencies:
crypto: ^3.0.0
sprintf: ^7.0.0
meta: ^1.10.0
meta: ^1.15.0
fixnum: ^1.1.0
dev_dependencies:
lints: ^4.0.0
test: ^1.24.9
test: ^1.25.8
funding:
- https://ko-fi.com/yulian
- https://paypal.me/yuliank
Expand Down
34 changes: 26 additions & 8 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ void main() {
var buffer = Uint8List(16);
var options = {'mSecs': testTime, 'nSecs': 0};

var wihoutBuffer = uuid.v1(options: options);
var withoutBuffer = uuid.v1(options: options);
uuid.v1buffer(buffer, options: options);

expect(Uuid.unparse(buffer), equals(wihoutBuffer));
expect(Uuid.unparse(buffer), equals(withoutBuffer));
});

test('Using Objects', () {
Expand Down Expand Up @@ -231,12 +231,12 @@ void main() {

test('Using buffers', () {
var buffer = Uint8List(16);
var wihoutBuffer =
var withoutBuffer =
uuid.v5(null, 'www.google.com', options: {'randomNamespace': false});
uuid.v5buffer(null, 'www.google.com', buffer,
options: {'randomNamespace': false});

expect(Uuid.unparse(buffer), equals(wihoutBuffer));
expect(Uuid.unparse(buffer), equals(withoutBuffer));
});

test('Using Objects', () {
Expand Down Expand Up @@ -337,10 +337,10 @@ void main() {
var buffer = Uint8List(16);
var options = V6Options(null, testTime, 0, null, null);

var wihoutBuffer = uuid.v6(config: options);
var withoutBuffer = uuid.v6(config: options);
uuid.v6buffer(buffer, config: options);

expect(Uuid.unparse(buffer), equals(wihoutBuffer));
expect(Uuid.unparse(buffer), equals(withoutBuffer));
});

test('Using Objects', () {
Expand Down Expand Up @@ -399,10 +399,10 @@ void main() {
final rand = MathRNG(seed: 1).generate();
var options = V7Options(testTime, rand);

var wihoutBuffer = uuid.v7(config: options);
var withoutBuffer = uuid.v7(config: options);
uuid.v7buffer(buffer, config: options);

expect(Uuid.unparse(buffer), equals(wihoutBuffer));
expect(Uuid.unparse(buffer), equals(withoutBuffer));
});

test('Using Objects', () {
Expand Down Expand Up @@ -548,6 +548,24 @@ void main() {
});

group('[Test Vectors]', () {
group('[UUID1]', () {
for (final testCase in {
'Tuesday, February 22, 2022 2:22:22.000000 PM GMT-05:00': [
1645557742000,
'C232AB00-9414-11EC-B3C8-9F6BDECED846'
],
}.entries) {
test(testCase.key, () {
var nodeId = <int>[0x9F, 0x6B, 0xDE, 0xCE, 0xD8, 0x46];
var clockSeq = (0xB3 << 8 | 0xC8) & 0x3ffff;
final uuid = Uuid().v1(
config: V1Options(
clockSeq, testCase.value[0] as int, 0, nodeId, null));
expect(uuid.toUpperCase(), equals(testCase.value[1]));
});
}
});

group('[UUID6]', () {
for (final testCase in {
'Tuesday, February 22, 2022 2:22:22.000000 PM GMT-05:00': [
Expand Down

0 comments on commit f9862c7

Please sign in to comment.