uuid

Cross platform, simple Uuid generation based on RFC4122 . Support versions are v1, v3, v4 and v5


Keywords
cross, game, haxe, uuid
License
MIT
Install
haxelib install uuid 2.1.0

Documentation

uuid

Cross-platform generation of UUID based on RFC-4122 .

Support for 1, 3, 4 and 5 versions of UUID. Port from node-uuid and using built-in Xorshift128+ for random generator.

Version 3 use Md5 for hash and version 5 use Sha1.

Random generator

For Uuid.v1() and Uuid.v4() you can pass any random function which return value between 0 and 255 . The default random generator is Xorshift128+ . Here is example for custom random function using Std.random

public function secureRandom():Int
{	
	return Std.random(256);
}

var uuid = Uuid.v1(secureRandom);

You can use Uuid to get any random number between some range, based on Xorshift128+ RNG, using the following code:

 var dice:Int = Uuid.randomFromRange(1,6);

API

Uuid.NIL The nil UUID string (all zeros).

Example: trace("Uuid:" +Uuid.NIL); // '00000000-0000-0000-0000-000000000000'

Uuid.parse(uuid:String, separator:String = "-"):Bytes Convert UUID string to Bytes

Uuid.stringify(data:Bytes,separator:String = "-"):String Convert Bytes to UUID string using separtor

Uuid.validate(uuid:String,separator:String = "-"):Bool Test a string to see if it is a valid UUID

Uuid.version(uuid:String,separator:String = "-"):Int Detect RFC version of a UUID

Uuid.randomFromRange(min:Int, max:Int):Int Return random number between min and max (included)

Usage

Version 1 (timestamp):

var uuid = Uuid.v1();
trace("UUID: " + uuid);

// Using custom separator
var uuid = Uuid.v1("_");
trace("UUID: " + uuid);

//Using predefined values
var node = Bytes.ofHex("010203040506"); // Array of 6 bytes, by default is random generated
var optClockSequence = 0x1a7f; // clock sequence (0 - 0x3fff)
var msecs = 1556526368;  // Time in milliseconds since unix Epoch
var optNsecs = 4500; // Additional time in nanoseconds (0-9999)
uuid = Uuid.v1(node,optClockSequence,msecs,optNsecs);
trace("UUID: " + uuid);

Version 3 (namespace):

var uuid = Uuid.v3("https://example.com/haxe",Uuid.URL); //namespace should be valid Uuid
trace("UUID: " + uuid);

uuid = Uuid.v3("The Cross-platform Toolkit");
trace("UUID: " + uuid);

uuid = Uuid.v3("The Cross-platform Toolkit",'7e9606ce-8af4-435b-89d6-66d6d885b97a');
trace("UUID: " + uuid);

Version 4 (random):

var uuid = Uuid.v4();
trace("UUID: " + uuid);

//Using custom separator - empty position
var uuid = Uuid.v4("");
trace("UUID: " + uuid);

Version 5 (namespace):

var uuid = Uuid.v5("https://example.com/haxe",Uuid.URL);
trace("UUID: " + uuid);

uuid = Uuid.v5("The Cross-platform Toolkit");
trace("UUID: " + uuid);

uuid = Uuid.v5("The Cross-platform Toolkit",'7e9606ce-8af4-435b-89d6-66d6d885b97a');
trace("UUID: " + uuid);