flutter_web_plugins/packages/dart-uuid/lib/uuidv4.dart

37 lines
1.2 KiB
Dart

class Uuidv4 {
/// v4() Generates a RNG version 4 UUID
///
/// By default it will generate a string based mathRNG, and will return
/// a string. If you wish to use crypto-strong RNG, pass in UuidUtil.cryptoRNG
///
/// 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
String v4({Map<String, dynamic> options}) {
options = (options != null) ? options : new Map<String, dynamic>();
// Use the built-in RNG or a custom provided RNG
var positionalArgs =
(options['positionalArgs'] != null) ? options['positionalArgs'] : [];
var namedArgs = (options['namedArgs'] != null)
? options['namedArgs'] as Map<Symbol, dynamic>
: const <Symbol, dynamic>{};
var rng = (options['rng'] != null)
? Function.apply(options['rng'], positionalArgs, namedArgs)
: _globalRNG();
// Use provided values over RNG
var rnds = (options['random'] != null) ? options['random'] : rng;
// per 4.4, set bits for version and clockSeq high and reserved
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
return unparse(rnds);
}
}