UUID

uuid

UUID - what is it?

UUID is a universally unique identifier which consists of 128-bit number used to identify information in computer systems. We can find UUID in many places, for instance:

  • NoSQL entries
  • MAC addresses
  • block patritions
  • BIOS
  • Microsoft's COM
  • LVM

and many more.

UUIDs are generated locally, without any central coordination that is why propability of duplicates is really low however keep in mind than is more than zero. Probability is about 0.00000000006 (6 × 10−11) but it depends on the methods used to generate them.

Versions

There are 6 versions of the UUIDs.

The rule of thumb

For Unique Random ID go with version 1 or version 4 (preferred), For Unique ID but related to parameter go with version 3 or version 5 (preferred).

version 0

This is, so called, nil UUID and all bits are set to zero.
Example: 00000000-0000-0000-0000-000000000000

version 1

Based on MAC address of computer and timestamp of generation. So it's quite uniq cause MAC shoud be uniq basend on OUI. However is it possible to change MAC and set time on the machine. Another caveat is that having such generated UUID it is possible to identify the MAC address and time of the generation of the UUID.

version 2

Similar to version 1, but not implemented in many libraries due to lact of spacification in the official RFC.

version 3

Based on namespace and name and hashed using md5 function.
Name spaces are predefined.

DNS: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
IOD: 6ba7b812-9dad-11d1-80b4-00c04fd430c8
URL: 6ba7b811-9dad-11d1-80b4-00c04fd430c8
X500: 6ba7b814-9dad-11d1-80b4-00c04fd430c8

But you can add new ones.
Warning! If you decide to generate UUIDs in the loop as a result you get the list of identical UUIDs

b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc
b6602c76-f86e-32a2-ab42-71dd055ad3cc

version 4

Based on random or pseudo-random numbers, where every bit is radomly calculated. This is the most commonly used version of UUID in the IT world.

Example, UUIDs v4 generated in the loop.

279c11e8-55ad-4579-bbe5-e1f2a17a3bc8
2ba33301-e52d-4831-842b-4f1ad51d5924
8dc051f4-5e70-4fc6-9af4-4b3a93746b09
f0207756-f003-4701-9737-5097aa7b413f
fe1bd0b0-5ca1-4157-b473-2c92dc57820b
be29e12b-4e24-41e3-84a4-e913f894d295
8910aae9-2077-40b2-8473-d90507aaa121
179e469e-cf9e-47ea-9344-797eceda347c
325d6e5d-5eae-4c8e-9cda-cdd07bf767a0

version 5

Based on namespace and name and hashed using sha-1 function.
Name spaces are predefined.

DNS: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
IOD: 6ba7b812-9dad-11d1-80b4-00c04fd430c8
URL: 6ba7b811-9dad-11d1-80b4-00c04fd430c8
X500: 6ba7b814-9dad-11d1-80b4-00c04fd430c8

And as it is with v3 you can add new ones.

Example

2fd21a62-3e57-59f0-ab5b-70c8d382d43f

Python

In the pyhton world there is nothing to install, module uuid has been boundled to the interpreter.

v1

Everything is out-oft-the box, just import module and run below code

import uuid
id1 = uuid.uuid1()
print(id1)

v3

Module is the same but function uuid3 requires the parameters namespace and name (paremeter)

for given set of paremeters uuid will be the same

import uuid
# namespae NAMESPACE_DNS
id3 = uuid.uuid3(uuid.NAMESPACE_DNS, 'parameter')
print(id3)
import uuid
# namespae NAMESPACE_X500
id3 = uuid.uuid3(uuid.NAMESPACE_X500, 'parameter')
print(id3)

namespace has to be UUID type so only for checking purpose we can use uuid1() as a parameter

import uuid
# namespae CUSTOM
id3a = uuid.uuid3(uuid.uuid1(), 'parameter')
print(id3a)
id3b = uuid.uuid3(id3a, 'parameter')
print(id3b)

v4

As it is with v1 just run uuid4() function from uuid module

import uuid

id4 = uuid.uuid4()
print(id4)

v5

Rules are similar to the v3

import uuid

id5 = uuid.uuid5(uuid.NAMESPACE_OID, 'parameter')
print(id5)

Java

Similarly, Java provides java.util.UUID package which is ready to use.

using java.util.UUID

I am using java.util.UUID which seems to be common way to generate id on java world however using it is the most challanging from langs I checked for this post.

v0

By putting zeros to UUID constructor we get v0

import java.util.UUID;

public class App
{
    public static void main( String[] args )
    {
        final String id0 = new UUID(0, 0).toString();
        System.out.println(id0);
    }
}

v4

This is the out-of-the-box function. So only v4 is provided by default.

import java.util.UUID;

public class App
{
    public static void main( String[] args )
    {
        final String id4 = UUID.randomUUID().toString();
        System.out.println(id4);
    }
}

Other versions are not provided directly.

using com.github.f4b6a3.uuid.UuidCreator

installation

    <dependency>
      <groupId>com.github.f4b6a3</groupId>
      <artifactId>uuid-creator</artifactId>
      <version>3.1.3</version>
    </dependency>

v1

import com.github.f4b6a3.uuid.UuidCreator;

public class App
{
    public static void main(String[] args )
    {
        final String id1 = UuidCreator.getTimeBased().toString();
        System.out.println(id1);
    }
}

v2

import java.util.UUID;

import com.github.f4b6a3.uuid.UuidCreator;
import com.github.f4b6a3.uuid.enums.UuidLocalDomain;

public class App
{
    public static void main( String[] args )
    {
        UUID id2 = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_ORG, 1);
        System.out.println(id2.toString());
    }
}

v3

import java.util.UUID;

import com.github.f4b6a3.uuid.UuidCreator;
import com.github.f4b6a3.uuid.enums.UuidNamespace;

public class App
{
    public static void main( String[] args )
    {
        UUID id3 = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, "paramater");
        System.out.println(id3.toString());
    }
}

v4

import java.util.UUID;

import com.github.f4b6a3.uuid.UuidCreator;

public class App
{
    public static void main( String[] args )
    {
        UUID id4 = UuidCreator.getRandomBased();
        System.out.println(id4.toString());
    }
}

v5

import java.util.UUID;

import com.github.f4b6a3.uuid.UuidCreator;
import com.github.f4b6a3.uuid.enums.UuidNamespace;

public class App
{
    public static void main( String[] args )
    {
        UUID id5 = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, "paramater");
        System.out.println(id5.toString());
    }
}

shell

Using uuidgen

There is a easy tool uuidgen which is able to generate all relevant versions.

v1

uuidgen --time

v3

uuidgen --md5 --namespace @url --name parameter

other defined namespaces: @dns, @url, @oid, or @x500

v4

uuidgen --random

v5

uuidgen --sha1 --namespace @dns --name parameter

using uuid

This is another handy tool or shell scripting.

v1

uuid -v1

v3

uuid -v3 ns:URL parameter

v4

uuid -v4

v5

uuid -v5 ns:OID parameter

javascript

In the js I used node v14.15.1 and npm 6.14.8 and syntax es2015 so called es6.

installation

Easy to install.

npm install uuid

v0

Here we have even v0 defined

import {NIL} from 'uuid';
console.log(NIL)

v1

No parameters needed for v1, it's not surprise

import {v1} from 'uuid';
console.log(v1())

v3

Not v3 as always two argumets are required, name and namespace.

import {v3} from 'uuid';
console.log(v3('parameter', v3.DNS))

Predefined namesapces (only):

  • v3.DNS
  • v3.URL

v4

import {v4} from 'uuid';
console.log(v4())

v5

Not v5 as always goes with two argumets as v3: name and namespace.

import {v5} from 'uuid';
console.log(v5('parameter', v5.URL))

Predefined namesapces (only):

  • v5.DNS
  • v5.URL

php

Installation: composer.json

{
    "require": {
        "ramsey/uuid": "4.1.1"
    }
}

v1

use RamseyUuidUuid;
$id = Uuid::uuid1();
echo $id;

v2

Yes, here we have v2 implemented too! uuid2 functino need security domain as a argument.

use RamseyUuidUuid;
$id = Uuid::uuid2(Uuid::DCE_DOMAIN_PERSON);
echo $id;

v3

Quite similar to other languages

use RamseyUuidUuid;
$id = Uuid::uuid3(Uuid::NAMESPACE_URL, 'parameter');
echo $id;

v4

The most common way.

use RamseyUuidUuid;
$id = Uuid::uuid4();
echo $id;

v5

Similar to v3.

use RamseyUuidUuid;
$id = Uuid::uuid5(Uuid::NAMESPACE_URL, 'parameter');
echo $id;

golang

In the golang world github.com/google/uuid is a best package for uuid generation which provides v0 and event v2 version.

v0

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    fmt.Print(uuid.Nil)
}

v1

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    var id1, _ = uuid.NewUUID()
    fmt.Print(id1)
}

v2

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {

    var id2, _ = uuid.NewDCESecurity(uuid.Person, 1)
    fmt.Print(id2)
}

v3

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    seed:= []byte("parameter")
    id3:= uuid.NewMD5(uuid.NameSpaceDNS, seed)
    fmt.Print(id3)
}

v4

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    id4, _:= uuid.NewRandom()
    fmt.Print(id4)
}

v5

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    seed:= []byte("parameter")
    id5:= uuid.NewSHA1(uuid.NameSpaceDNS, seed)
    fmt.Print(id5)
}

More info

1 Comment

  1. I’ve been browsing on-line greater than 3 hours lately, yet I by no means discovered
    any fascinating article like yours. It is pretty value enough for me.

    Personally, if all website owners and bloggers made good
    content as you did, the web will be much more helpful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *