dart-uuid

repository·main·Indexed 19 days ago

https://github.com/daegalus/dart-uuid

A Dart package for generating and parsing RFC4122 and RFC9562 compliant UUIDs. Supports Version 1, 4, and 5 (RFC4122) and Version 6, 7, and 8 (RFC9562) across web, server, and Flutter environments using cryptographically strong random number generation. Includes APIs for validating and parsing 128-bit hexadecimal values.

Tokens
564
Snippets
3
Records
3
Agent score
16%

What's inside dart-uuid

  1. Install dart-uuid via pubspec

    main

    To use dart-uuid in your project, add it to your pubspec.yaml dependencies. After adding the entry, run pub get (or pub install) to download the package.

    Note: Version 4.x.x is a complete redesign but maintains API compatibility with 3.x.

    dependencies:
      uuid: ^4.6.0
  2. Generate RFC4122 and RFC9562 UUIDs

    main

    To generate UUIDs, instantiate the Uuid class and call the method corresponding to the desired version. This package supports:

    • RFC4122: Version 1 (time-based), Version 4 (random), and Version 5 (namespace-name-sha1-based).
    • RFC9562: Version 6, Version 7, and Version 8.

    All platforms (web, server, and flutter) use cryptographically strong random number generation.

    import 'package:uuid/uuid.dart';
    
    var uuid = Uuid();
    
    // Generate a v1 (time-based) id
    uuid.v1();
    
    // Generate a v4 (random) id
    uuid.v4();
    
    // Generate a v5 (namespace-name-sha1-based) id
    uuid.v5(Namespace.url.value, 'www.google.com');
  3. Parse 128-bit hexadecimal values without enforcing UUID constraints

    main

    If you need to validate or parse a 128-bit hexadecimal value where you do not want to enforce specific UUID version or variant bits, use the explicit format APIs. These methods allow you to handle generic 128-bit values.

    • Uuid.isValidUUIDFormat(fromString: value): Returns true if the string matches the expected hexadecimal format.
    • Uuid.parseHex128(value, {bool noDashes = false}): Parses the string into 16 bytes. Use noDashes: true if the input string does not contain hyphens.
    const value = '019f13f5-53cb-b219-ca3e-4b569376f32b';
    
    Uuid.isValidUUIDFormat(fromString: value); // true
    Uuid.parseHex128(value); // 16 bytes
    
    const withoutDashes = '019f13f553cbb219ca3e4b569376f32b';
    Uuid.parseHex128(withoutDashes, noDashes: true); // 16 bytes