Globally Unique Identifier (GUID) is a pseudo-random string which consists of 32 letters, numbers (0-9), and 4 hyphens to separate letters. These letters are randomly generated. In probability theory, this value is unique and can be used as a secret and session cookie, etc.
Example GUID
The following line provides a GUID. as we can see there are 4 dashes that separate the values into 5 parts. The first part consist of 6 letters, second part 4, third part 4, forth part 4, and the last fifth part 12 letters. They are generated randomly.
5c981150-9d1d-11e8-98fa-000c297195d3
Generate GUID In Linux
Linux provides a lot of different tools to generate GUID. But one of the most simple and default installed tool is uuidgen
. We just issue the command like below.
$ uuidgen
Generate GUID In Windows
Windows operating systems like Server 2008, 20012, 2016, 7, 8, 10 have the ability to generate GUID in PowerShell, 3rd party tools, etc. But the most integrated and fast way is using PowerShell which actually uses .Net libraries we have already examined in Generating GUID with C#
.
[guid]::NewGuid()
Generate GUID In Java
Java programming language and software development kit provides a lot of different types of random number generation and format functions. We can use util
class randomUUID()
function like below.
java.util.UUID.randomUUID();
Generate GUID In PHP
PHP has different ways to generate GUID. PHP running on the Windows system can use com_create_guid()
function but in Linux, we need some external help. We can create a function that will generate random numbers in the specified range and length. We will create a function named GUID()
like below and inject com_create_guid()
function too.
<?php
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535),
mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
echo GUID();
?>
Another way is using openssl
functions with some convert.
bin2hex(openssl_random_pseudo_bytes(16));
Generate GUID In C#
C# is a very feature-rich and reliable programming language with useful libraries. C# provides the NewGuid()
method in order to create some GUID value. The following snippet will create a GUID value
using System;
class Example
{
public static void Main()
{
Guid g;
g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());
}
}
Generate GUID In Python
Python provides uuid
module which contains a lot of different functions about the Unique Identifier. We can use uuid1()
to generate GUID easily in a single line.
$ python -c 'import uuid; print str(uuid.uuid1())'
Hi,
There is a mistake in definition, it should be:
First part consist of 8 letters, ….. the last fifth part 12 letters.
Thanks for your suggestion. I have corrected the mistake.
The Java, Python, c, javaScript is used for the Globally identifier purpose. The GUID is the 32-bit string. We can use this in the windows also.