Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to Generate GUID

How to Generate GUID – Java, PHP, Flutter

Posted on February 24, 2021June 7, 2025 by Toma Velev

In this article you’ll get samples of how to Generate Globally Unique Identifier or GUID in Java, PHP and Flutter.

Why Use it?

Globally Unique Identifiers have certain benefits over number/integer based auto incrementing keys and ids.

  • They are uniquely and randomly created on multiple devices and servers. This is useful in multi-server architectures and applications that embrace synchronization and independence simultaneously.
  • When GUIDs are part of a URL address of some record, they add another layer of security. You will not be able to just manually bump up or down an integer ID.

Generate Java GUID

The Java Programming Language has build-in method for this feature. I’ve extracted a static method like this:

package uuid;
import java.util.UUID;
public class Tssstete {
	public static void main(String[] args) {
		System.out.println(GUID());
	}
	private static String GUID() {
		return UUID.randomUUID().toString();
	}
}

Generate GUID with Flutter:

Flutter has a dart.js plugin for generating Universally Unique Identifiers. As such – it is usable on Web, Mobile Platforms, and it will also be usable – probably – out of the box on the Desktop Platforms.

Install it: 
dependencies:
  uuid: 2.2.2
Import it:
import 'package:uuid/uuid.dart';
And use it:
var uuid = Uuid();
uuid.v4();

PHP uuid

To be honest, I’ve copied it from Stack Overflow, but, it does the job:

<?php

function get_guid() {
    $data = PHP_MAJOR_VERSION < 7 ? openssl_random_pseudo_bytes(16) : random_bytes(16);
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // Set version to 0100
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // Set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
echo get_guid();
?>

JavaScript

At last – the language that will reach potentially all devices is JavaScript (snippet copied from https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid ):

function uuidv4() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

Demo of the JavaScript Code: https://programtom.com/dev_examples/guid/guild_js/

A Demonstration of the code is recorded on YouTube: https://youtu.be/0n3OmtiZDOc

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)