generate method

String generate(
  1. String? namespace,
  2. String? name, {
  3. V5Options? options,
})

v5() Generates a namspace & name-based version 5 UUID

By default it will generate a string based on a provided uuid namespace and name, and will return a string.

The first argument is an options map that takes various configuration options detailed in the readme.

http://tools.ietf.org/html/rfc4122.html#section-4.4

Implementation

String generate(String? namespace, String? name, {V5Options? options}) {
  // Check if user wants a random namespace generated by v4() or a NIL namespace.
  var useRandom = options?.randomNamespace ?? true;

  // If useRandom is true, generate UUIDv4, else use NIL
  var blankNS = useRandom
      ? UuidV4(goptions: goptions).generate(options: options?.v4options)
      : Namespace.nil.value;

  // Use provided namespace, or use whatever is decided by options.
  namespace = (namespace != null) ? namespace : blankNS;

  // Use provided name,
  name = (name != null) ? name : '';

  // Convert namespace UUID to Byte List
  var bytes = UuidParsing.parse(namespace);

  // Convert name to UTF-8 bytes
  var nameBytes = utf8.encode(name);

  // Generate SHA1 using namespace concatenated with name
  var hashBytes = crypto.sha1.convert([...bytes, ...nameBytes]).bytes;

  // per 4.4, set bits for version and clockSeq high and reserved
  hashBytes[6] = (hashBytes[6] & 0x0f) | 0x50;
  hashBytes[8] = (hashBytes[8] & 0x3f) | 0x80;

  return UuidParsing.unparse(hashBytes.sublist(0, 16));
}