forked from nalgeon/uuidv7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuidv7.java
43 lines (35 loc) · 1.24 KB
/
uuidv7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.UUID;
public class UUIDv7 {
private static final SecureRandom random = new SecureRandom();
public static UUID randomUUID() {
byte[] value = randomBytes();
ByteBuffer buf = ByteBuffer.wrap(value);
long high = buf.getLong();
long low = buf.getLong();
return new UUID(high, low);
}
public static byte[] randomBytes() {
// random bytes
byte[] value = new byte[16];
random.nextBytes(value);
// current timestamp in ms
long timestamp = System.currentTimeMillis();
// timestamp
value[0] = (byte) ((timestamp >> 40) & 0xFF);
value[1] = (byte) ((timestamp >> 32) & 0xFF);
value[2] = (byte) ((timestamp >> 24) & 0xFF);
value[3] = (byte) ((timestamp >> 16) & 0xFF);
value[4] = (byte) ((timestamp >> 8) & 0xFF);
value[5] = (byte) (timestamp & 0xFF);
// version and variant
value[6] = (byte) ((value[6] & 0x0F) | 0x70);
value[8] = (byte) ((value[8] & 0x3F) | 0x80);
return value;
}
public static void main(String[] args) {
var uuid = UUIDv7.randomUUID();
System.out.println(uuid);
}
}