---
# System prepended metadata

title: Java UUID4
tags: [Spring boot, UUID]

---

# Java UUID4
### 1. UUID Types
##### A typical UID is displayed in 5 groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and 4 hyphens).
>123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
* The version field holds a value that describes the type of this UUID. There are five different basic types of UUIDs.
    1. Time-Based UUID (Version 1) – generated from a time and a node id
    2. DCE (Distributed Computing Environment) security (Version 2) – generated from an identifier (usually a group or user id), time, and a node id
    3. Name-based (Version 3) – generated by MD5 (128 bits) hashing of a namespace identifier and name
    4. Randomly generated UUIDs (Version 4) – generated using a random or pseudo-random number
    5. Name-based using SHA-1 hashing (Version 5) Recommended – generated by SHA-1 (160 bits) hashing of a namespace identifier and name. Java does not provide its implementation. We shall create our own.

### 2. Java UUID Example
>UUID.randomUUID() – Version 4
```java=
UUID uuid = UUID.randomUUID();
         
System.out.println(uuid);
System.out.println(uuid.variant());     //2
System.out.println(uuid.version());     //4

//console
17e3338d-344b-403c-8a87-f7d8006d6e33
2
4
```
>Generate Version 5 UUID
```java=
//UUID5.java
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
 
public class UUID5 
{
    public static UUID fromUTF8(String name) {
        return fromBytes(name.getBytes(Charset.forName("UTF-8")));
    }
     
    private static UUID fromBytes(byte[] name) {
        if (name == null) {
            throw new NullPointerException("name == null");
        }
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            return makeUUID(md.digest(name), 5);
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(e);
        }
    }
     
    private static UUID makeUUID(byte[] hash, int version) {
        long msb = peekLong(hash, 0, ByteOrder.BIG_ENDIAN);
        long lsb = peekLong(hash, 8, ByteOrder.BIG_ENDIAN);
        // Set the version field
        msb &= ~(0xfL << 12);
        msb |= ((long) version) << 12;
        // Set the variant field to 2
        lsb &= ~(0x3L << 62);
        lsb |= 2L << 62;
        return new UUID(msb, lsb);
    }
     
    private static long peekLong(final byte[] src, final int offset, final ByteOrder order) {
        long ans = 0;
        if (order == ByteOrder.BIG_ENDIAN) {
            for (int i = offset; i < offset + 8; i += 1) {
                ans <<= 8;
                ans |= src[i] & 0xffL;
            }
        } else {
            for (int i = offset + 7; i >= offset; i -= 1) {
                ans <<= 8;
                ans |= src[i] & 0xffL;
            }
        }
        return ans;
    }
}
```
>Java Version 5 UUID Example
```java=
UUID uuid = UUID5.fromUTF8("954aac7d-47b2-5975-9a80-37eeed186527");
 
System.out.println(uuid);
System.out.println(uuid.variant());
System.out.println(uuid.version());

//console
d1d16b54-9757-5743-86fa-9ffe3b937d78
2
5
```


資料來源:
[Java UUID Generator Example](https://howtodoinjava.com/java/java-misc/java-uuid/)


###### tags: `Spring boot` `UUID`