1 package org
.argeo
.api
.acr
.uuid
;
3 import static java
.lang
.System
.Logger
.Level
.DEBUG
;
4 import static java
.lang
.System
.Logger
.Level
.WARNING
;
6 import java
.lang
.System
.Logger
;
7 import java
.security
.DrbgParameters
;
8 import java
.security
.NoSuchAlgorithmException
;
9 import java
.security
.SecureRandom
;
10 import java
.time
.Clock
;
11 import java
.util
.UUID
;
14 * Simple implementation of an {@link UuidFactory}, which can be used as a base
15 * class for more optimised implementations.
17 * @see https://datatracker.ietf.org/doc/html/rfc4122
19 public class SimpleUuidFactory
extends AbstractAsyncUuidFactory
{
20 private final static Logger logger
= System
.getLogger(SimpleUuidFactory
.class.getName());
21 public final static UuidFactory DEFAULT
= new SimpleUuidFactory(null, -1, null);
23 // private NodeId macAddressNodeId;
24 // private NodeId defaultNodeId;
25 private byte[] macAddressNodeId
;
26 private byte[] defaultNodeId
;
28 public SimpleUuidFactory(byte[] nodeId
, int offset
, Clock clock
) {
29 byte[] hardwareAddress
= getHardwareAddress();
30 // macAddressNodeId = hardwareAddress != null ? new NodeId(hardwareAddress, 0) : null;
31 macAddressNodeId
= toNodeId(hardwareAddress
, 0);
33 // defaultNodeId = nodeId != null ? new NodeId(nodeId, offset) : macAddressNodeId;
34 defaultNodeId
= nodeId
!= null ?
toNodeId(nodeId
, offset
) : toNodeId(macAddressNodeId
, 0);
35 if (defaultNodeId
== null)
36 throw new IllegalStateException("No default node id specified");
40 protected SecureRandom
newSecureRandom() {
41 SecureRandom secureRandom
;
43 secureRandom
= SecureRandom
.getInstance("DRBG",
44 DrbgParameters
.instantiation(256, DrbgParameters
.Capability
.PR_AND_RESEED
, "UUID".getBytes()));
45 } catch (NoSuchAlgorithmException e
) {
47 logger
.log(DEBUG
, "DRBG secure random not found, using strong");
48 secureRandom
= SecureRandom
.getInstanceStrong();
49 } catch (NoSuchAlgorithmException e1
) {
50 logger
.log(WARNING
, "No strong secure random was found, using default");
51 secureRandom
= new SecureRandom();
58 * TIME-BASED (version 1)
62 public UUID
newTimeUUIDwithMacAddress() {
63 if (macAddressNodeId
== null)
64 throw new UnsupportedOperationException("No MAC address is available");
65 return newTimeUUID(timeUuidState
.useTimestamp(), timeUuidState
.getClockSequence(), macAddressNodeId
, 0);
69 public UUID
newTimeUUID() {
70 return newTimeUUID(timeUuidState
.useTimestamp(), timeUuidState
.getClockSequence(), defaultNodeId
, 0);
77 // public UUID randomUUID(Random random) {
78 // return newRandomUUID(random);
82 // public UUID randomUUID() {
83 // return randomUUID(secureRandom);
86 // static class NodeId extends ThreadLocal<byte[]> {
87 // private byte[] source;
88 // private int offset;
90 // public NodeId(byte[] source, int offset) {
91 // Objects.requireNonNull(source);
92 // this.source = source;
93 // this.offset = offset;
94 // if (offset < 0 || offset + 6 > source.length)
95 // throw new ArrayIndexOutOfBoundsException(offset);
99 // protected byte[] initialValue() {
100 // byte[] value = new byte[6];
101 // System.arraycopy(source, offset, value, 0, 6);