v5 method

String v5(
  1. String? namespace,
  2. String? name, {
  3. Map<String, dynamic>? options,
})

v5() Generates a namespace & 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 v5(String? namespace, String? name, {Map<String, dynamic>? options}) {
  options ??= {};

  // Check if user wants a random namespace generated by v4() or a NIL namespace.
  var useRandom = (options['randomNamespace'] != null) ? options['randomNamespace'] : true;

  // If useRandom is true, generate UUIDv4, else use NIL
  var blankNS = useRandom ? v4() : namespaceNil;

  // 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 = parse(namespace);

  // Convert name to a list of bytes
  var nameBytes = <int>[];
  for (var singleChar in name.codeUnits) {
    nameBytes.add(singleChar);
  }

  // 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 unparse(hashBytes.sublist(0, 16));
}