1 | class UUIDGenerator {↵ | | 1 | class UUIDGenerator {↵
|
|
2 | /**↵ | | 2 | /**↵
|
3 | * random number generator for UUID generation↵ | | 3 | * random number generator for UUID generation↵
|
4 | */↵ | | 4 | */↵
|
5 | private final SecureRandom secRand = new SecureRandom();↵ | | 5 | private final SecureRandom secRand = new SecureRandom();↵
|
|
6 | /**↵ | | 6 | /**↵
|
7 | * 128-bit buffer for use with secRand↵ | | 7 | * 128-bit buffer for use with secRand↵
|
8 | */↵ | | 8 | */↵
|
9 | private final byte[] secRandBuf16 = new byte[16];↵ | | 9 | private final byte[] secRandBuf16 = new byte[16];↵
|
|
10 | public UUIDGenerator() {↵ | | 10 | public UUIDGenerator() {↵
|
11 | super();↵ | | 11 | super();↵
|
12 | }↵ | | 12 | }↵
|
|
13 | /**↵ | | 13 | /**↵
|
14 | * @return uuid as String↵ | | 14 | * @return uuid as String↵
|
15 | */↵ | | 15 | */↵
|
16 | public String newUUID() {↵ | | 16 | public String newUUID() {↵
|
17 | secRand.nextBytes(secRandBuf16);↵ | | 17 | secRand.nextBytes(secRandBuf16);↵
|
18 | secRandBuf16[6] &= 0x0f;↵ | | 18 | secRandBuf16[6] &= 0x0f;↵
|
19 | secRandBuf16[6] |= 0x40; /* version 4 */↵ | | 19 | secRandBuf16[6] |= 0x40; /* version 4 */↵
|
20 | secRandBuf16[8] &= 0x3f;↵ | | 20 | secRandBuf16[8] &= 0x3f;↵
|
21 | secRandBuf16[8] |= 0x80; /* IETF variant */↵ | | 21 | secRandBuf16[8] |= 0x80; /* IETF variant */↵
|
22 | secRandBuf16[10] |= 0x80; /* multicast bit */↵ | | 22 | secRandBuf16[10] |= 0x80; /* multicast bit */↵
|
23 | long mostSig = 0;↵ | | 23 | long mostSig = 0;↵
|
24 | for (int i = 0; i < 8; i++) {↵ | | 24 | for (int i = 0; i < 8; i++) {↵
|
25 | mostSig = (mostSig << 8) | (secRandBuf16[i] & 0xff);↵ | | 25 | mostSig = (mostSig << 8) | (secRandBuf16[i] & 0xff);↵
|
26 | }↵ | | 26 | }↵
|
27 | long leastSig = 0;↵ | | 27 | long leastSig = 0;↵
|
28 | for (int i = 8; i < 16; i++) {↵ | | 28 | for (int i = 8; i < 16; i++) {↵
|
29 | leastSig = (leastSig << 8) | (secRandBuf16[i] & 0xff);↵ | | 29 | leastSig = (leastSig << 8) | (secRandBuf16[i] & 0xff);↵
|
30 | }↵ | | 30 | }↵
|
31 | return (digits(mostSig >> 32, 8) + "-" + digits(mostSig >> 16, 4) + "-"↵ | | 31 | return (digits(mostSig >> 32, 8) + "-" + digits(mostSig >> 16, 4) + "-" //$NON-NLS-1$//$NON-NLS-2$↵
|
32 | + digits(mostSig, 4) + "-" + digits(leastSig >> 48, 4) + "-" + digits(↵ | | 32 | + digits(mostSig, 4) + "-" + digits(leastSig >> 48, 4) + "-" + digits( //$NON-NLS-1$//$NON-NLS-2$↵
|
33 | leastSig, 12));↵ | | 33 | leastSig, 12));↵
|
34 | }↵ | | 34 | }↵
|
|
35 | /** Returns val represented by the specified number of hex digits. */↵ | | 35 | /** Returns val represented by the specified number of hex digits. */↵
|
36 | private static String digits(long val, int digits) {↵ | | 36 | private static String digits(long val, int digits) {↵
|
37 | long hi = 1L << (digits * 4);↵ | | 37 | long hi = 1L << (digits * 4);↵
|
38 | return Long.toHexString(hi | (val & (hi - 1))).substring(1);↵ | | 38 | return Long.toHexString(hi | (val & (hi - 1))).substring(1);↵
|
39 | }↵ | | 39 | }↵
|
|
40 | } | | 40 | }
|