diff --git a/CHANGELOG.md b/CHANGELOG.md index e9115e8..4744328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +v4.2.2 + +* Fix CryptoRNG on Web generating a random number 0 always +* Add NoDashes support to the validator so that it validates UUIDs that don't have dashes but are otherwise valid. + v4.2.1 * Lower `meta` dependency to 1.9.1 so that it is compatible with Flutter Stable 3.13 diff --git a/lib/rng.dart b/lib/rng.dart index 5233f11..f008fb8 100644 --- a/lib/rng.dart +++ b/lib/rng.dart @@ -53,7 +53,7 @@ class CryptoRNG extends RNG { final b = Uint8List(16); for (var i = 0; i < 16; i += 4) { - var k = _secureRandom.nextInt(1 << 32); + var k = _secureRandom.nextInt(pow(2, 32).toInt()); b[i] = k; b[i + 1] = k >> 8; b[i + 2] = k >> 16; diff --git a/lib/uuid.dart b/lib/uuid.dart index a776c4f..e826180 100644 --- a/lib/uuid.dart +++ b/lib/uuid.dart @@ -128,11 +128,13 @@ class Uuid { static bool isValidUUID( {String fromString = '', Uint8List? fromByteList, - ValidationMode validationMode = ValidationMode.strictRFC4122}) { + ValidationMode validationMode = ValidationMode.strictRFC4122, + bool noDashes = false}) { return UuidValidation.isValidUUID( fromString: fromString, fromByteList: fromByteList, - validationMode: validationMode); + validationMode: validationMode, + noDashes: noDashes); } /// Generates a time-based version 1 UUID diff --git a/lib/validation.dart b/lib/validation.dart index c28601e..18ed3ee 100644 --- a/lib/validation.dart +++ b/lib/validation.dart @@ -10,7 +10,8 @@ class UuidValidation { static bool isValidUUID( {String fromString = '', Uint8List? fromByteList, - ValidationMode validationMode = ValidationMode.strictRFC4122}) { + ValidationMode validationMode = ValidationMode.strictRFC4122, + bool noDashes = false}) { if (fromByteList != null) { fromString = UuidParsing.unparse(fromByteList); } @@ -20,7 +21,12 @@ class UuidValidation { } // If its not 36 characters in length, don't bother (including dashes). - if (fromString.length != 36) { + if (!noDashes && fromString.length != 36) { + return false; + } + + // If its not 32 characters in length, don't bother (excluding excluding). + if (noDashes && fromString.length != 32) { return false; } @@ -28,16 +34,18 @@ class UuidValidation { switch (validationMode) { case ValidationMode.strictRFC4122: { - const pattern = - r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'; + var pattern = (noDashes) + ? r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-8][0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$' + : r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'; final regex = RegExp(pattern, caseSensitive: false, multiLine: true); final match = regex.hasMatch(fromString.toLowerCase()); return match; } case ValidationMode.nonStrict: { - const pattern = - r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$'; + var pattern = (noDashes) + ? r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-8][0-9a-f]{3}-?[0-9a-f]{4}-?[0-9a-f]{12}$' + : r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$'; final regex = RegExp(pattern, caseSensitive: false, multiLine: true); final match = regex.hasMatch(fromString.toLowerCase()); return match; @@ -60,11 +68,13 @@ class UuidValidation { static void isValidOrThrow( {String fromString = '', Uint8List? fromByteList, - ValidationMode validationMode = ValidationMode.strictRFC4122}) { + ValidationMode validationMode = ValidationMode.strictRFC4122, + bool noDashes = false}) { final isValid = isValidUUID( fromString: fromString, fromByteList: fromByteList, - validationMode: validationMode); + validationMode: validationMode, + noDashes: noDashes); if (!isValid) { // let's check if it is a non RFC4122 uuid and help the developer @@ -72,7 +82,8 @@ class UuidValidation { final isValidNonStrict = isValidUUID( fromString: fromString, fromByteList: fromByteList, - validationMode: ValidationMode.nonStrict); + validationMode: ValidationMode.nonStrict, + noDashes: noDashes); if (isValidNonStrict) { throw FormatException( diff --git a/pubspec.yaml b/pubspec.yaml index 0e62637..d858276 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: uuid -version: 4.2.1 +version: 4.2.2 description: > RFC4122 (v1, v4, v5, v6, v7, v8) UUID Generator and Parser for Dart documentation: https://daegalus.github.io/dart-uuid/index.html diff --git a/test/uuid_test.dart b/test/uuid_test.dart index a55b167..cbda671 100644 --- a/test/uuid_test.dart +++ b/test/uuid_test.dart @@ -510,6 +510,20 @@ void main() { }); }); + group('[Validation Test]', () { + test('Dashes UUID', () { + const validUUID = '87cd4eb3-cb88-449b-a1da-e468fd829310'; + expect(Uuid.isValidUUID(fromString: validUUID, noDashes: true), false); + expect(Uuid.isValidUUID(fromString: validUUID), true); + }); + test('No Dashes UUID', () { + const validNoDashesUUID = '87cd4eb3cb88449ba1dae468fd829310'; + expect(Uuid.isValidUUID(fromString: validNoDashesUUID, noDashes: true), + true); + expect(Uuid.isValidUUID(fromString: validNoDashesUUID), false); + }); + }); + group('[Test Vectors]', () { group('[UUID6]', () { for (final testCase in {