v5 method

String v5 (String namespace, String name, { Map<String, dynamic> 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 v5(String namespace, String name, {Map<String, dynamic> options}) {
  options = (options != null) ? options : new Map();

  // 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() : NAMESPACE_NIL;

  // 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 = new List<int>();
  for (var singleChar in name.codeUnits) {
    nameBytes.add(singleChar);
  }

  // Generate SHA1 using namespace concatenated with name
  List hashBytes =
      sha1.convert(new List.from(bytes)..addAll(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);
}