Skip to content

Commit 99c7a91

Browse files
Anand kumarpivovarit
authored andcommitted
guide to uuid generation in java (eugenp#1879)
* guide to uuid generation in java * updated readme.md
1 parent 7c65366 commit 99c7a91

2 files changed

Lines changed: 119 additions & 1 deletion

File tree

‎core-java/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
103103
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
104104
- [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue)
105+
- [Guide to UUID in JAVA] (http://www.baeldung.com/guide-to-uuid-in-java)
105106
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path)
106107
- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend)
107108
- [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue)
@@ -111,4 +112,3 @@
111112
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
112113
- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator)
113114
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
114-
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.baeldung.uuid;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.UUID;
7+
8+
public class UUIDGenerator {
9+
10+
/**
11+
* These are predefined UUID for name spaces
12+
*/
13+
private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
14+
private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
15+
private static final String NAMESPACE_OID = "6ba7b812-9dad-11d1-80b4-00c04fd430c8";
16+
private static final String NAMESPACE_X500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8";
17+
18+
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
19+
20+
public static void main(String[] args) {
21+
try {
22+
System.out.println("Type 3 : " + generateType3UUID(NAMESPACE_DNS, "google.com"));
23+
System.out.println("Type 4 : " + generateType4UUID());
24+
System.out.println("Type 5 : " + generateType5UUID(NAMESPACE_URL, "google.com"));
25+
System.out.println("Unique key : " + generateUniqueKeysWithUUIDAndMessageDigest());
26+
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
31+
/**
32+
* Type 4 UUID Generation
33+
*/
34+
public static UUID generateType4UUID() {
35+
UUID uuid = UUID.randomUUID();
36+
return uuid;
37+
}
38+
39+
/**
40+
* Type 3 UUID Generation
41+
*
42+
* @throws UnsupportedEncodingException
43+
*/
44+
public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException {
45+
String source = namespace + name;
46+
byte[] bytes = source.getBytes("UTF-8");
47+
UUID uuid = UUID.nameUUIDFromBytes(bytes);
48+
return uuid;
49+
}
50+
51+
/**
52+
* Type 5 UUID Generation
53+
*
54+
* @throws UnsupportedEncodingException
55+
*/
56+
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException {
57+
String source = namespace + name;
58+
byte[] bytes = source.getBytes("UTF-8");
59+
UUID uuid = type5UUIDFromBytes(bytes);
60+
return uuid;
61+
}
62+
63+
64+
public static UUID type5UUIDFromBytes(byte[] name) {
65+
MessageDigest md;
66+
try {
67+
md = MessageDigest.getInstance("SHA-1");
68+
} catch (NoSuchAlgorithmException nsae) {
69+
throw new InternalError("MD5 not supported", nsae);
70+
}
71+
byte[] bytes = md.digest(name);
72+
bytes[6] &= 0x0f; /* clear version */
73+
bytes[6] |= 0x50; /* set to version 5 */
74+
bytes[8] &= 0x3f; /* clear variant */
75+
bytes[8] |= 0x80; /* set to IETF variant */
76+
return constructType5UUID(bytes);
77+
}
78+
79+
private static UUID constructType5UUID(byte[] data) {
80+
long msb = 0;
81+
long lsb = 0;
82+
assert data.length == 16 : "data must be 16 bytes in length";
83+
84+
for (int i=0; i<8; i++)
85+
msb = (msb << 8) | (data[i] & 0xff);
86+
87+
for (int i=8; i<16; i++)
88+
lsb = (lsb << 8) | (data[i] & 0xff);
89+
return new UUID(msb, lsb);
90+
}
91+
92+
93+
/**
94+
* Unique Keys Generation Using Message Digest and Type 4 UUID
95+
*
96+
* @throws NoSuchAlgorithmException
97+
* @throws UnsupportedEncodingException
98+
*/
99+
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
100+
MessageDigest salt = MessageDigest.getInstance("SHA-256");
101+
salt.update(UUID.randomUUID()
102+
.toString()
103+
.getBytes("UTF-8"));
104+
String digest = bytesToHex(salt.digest());
105+
return digest;
106+
}
107+
108+
public static String bytesToHex(byte[] bytes) {
109+
char[] hexChars = new char[bytes.length * 2];
110+
for (int j = 0; j < bytes.length; j++) {
111+
int v = bytes[j] & 0xFF;
112+
hexChars[j * 2] = hexArray[v >>> 4];
113+
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
114+
}
115+
return new String(hexChars);
116+
}
117+
118+
}

0 commit comments

Comments
 (0)