-
Notifications
You must be signed in to change notification settings - Fork 53
Adding support for randomUUID #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,28 @@ final class _RandomImpl implements RandomImpl { | |
dest.setAll(0, out.asTypedList(dest.length)); | ||
}); | ||
} | ||
|
||
@override | ||
String randomUUID() { | ||
return _Scope.sync((scope) { | ||
final out = scope<ffi.Uint8>(16); | ||
_checkOp(ssl.RAND_bytes(out, 16) == 1); | ||
|
||
var bytes = out.asTypedList(16); | ||
|
||
// Set the 4 most significant bits of bytes[6] to 0100. | ||
bytes[6] = (bytes[6] & 0x0F) | 0x40; | ||
|
||
// Set the 2 most significant bits of bytes[8] to 10. | ||
bytes[8] = (bytes[8] & 0x3F) | 0x80; | ||
|
||
return bytes | ||
.map((e) => e.toRadixString(16).padLeft(2, '0')) | ||
.join() | ||
.replaceAllMapped( | ||
RegExp(r'(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})'), | ||
(m) => '${m[1]}-${m[2]}-${m[3]}-${m[4]}-${m[5]}', | ||
); | ||
Comment on lines
+49
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've done this other places the classical implementation is:
Maybe, using |
||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,14 @@ void isNotAllZero(TypedData data) { | |
check(data.buffer.asUint8List().any((b) => b != 0)); | ||
} | ||
|
||
void isValidUUID(String uuid) { | ||
check(uuid.length == 36); | ||
check(uuid[8] == '-'); | ||
check(uuid[13] == '-'); | ||
check(uuid[18] == '-'); | ||
check(uuid[23] == '-'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the bits too |
||
} | ||
|
||
void main() => tests().runTests(); | ||
|
||
/// Tests, exported for use in `../run_all_tests.dart`. | ||
|
@@ -83,5 +91,16 @@ List<({String name, Future<void> Function() test})> tests() { | |
isNotAllZero(data); | ||
}); | ||
|
||
test('randomUUID: 100 UUIDs', () async { | ||
final uuids = <String>{}; | ||
for (var i = 0; i < 100; i++) { | ||
final uuid = randomUUID(); | ||
isValidUUID(uuid); | ||
check(uuids.add(uuid)); | ||
} | ||
|
||
check(uuids.length == 100); | ||
}); | ||
|
||
return tests; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,18 @@ void fillRandomBytes( | |
|
||
webCryptImpl.random.fillRandomBytes(destination); | ||
} | ||
|
||
/// Generate a cryptographically random UUID. | ||
/// | ||
/// **Example** | ||
/// ```dart | ||
/// import 'package:webcrypto/webcrypto.dart'; | ||
/// | ||
/// // Generate a random UUID. | ||
/// void main() { | ||
/// final uuid = randomUUID(); | ||
/// print(uuid); | ||
/// } | ||
/// | ||
/// ``` | ||
String randomUUID() => webCryptImpl.random.randomUUID(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This generates a UUIDv4, there is a specification let's explain that: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of the function in Dart should follow conventions:
We could choose to just call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HamdaanAliQuatil any thoughts on the name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
To add to this - based on the last discussion on If someday webcrypto introduces ULID, it will likely be another in another class and that would be an issue for another day. As for now, and for the near future as well, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, we could just use
fillRandomBytes
, then we have less FFI stuff going on.