From 99c7a91587f746033b6578fd94ff3080df74cd13 Mon Sep 17 00:00:00 2001 From: Anand kumar Date: Sun, 28 May 2017 15:36:56 +0530 Subject: [PATCH] guide to uuid generation in java (#1879) * guide to uuid generation in java * updated readme.md --- core-java/README.md | 2 +- .../java/com/baeldung/uuid/UUIDGenerator.java | 118 ++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java diff --git a/core-java/README.md b/core-java/README.md index 1de57c9311..6dd43fe6d9 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -102,6 +102,7 @@ - [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request) - [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) - [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue) +- [Guide to UUID in JAVA] (http://www.baeldung.com/guide-to-uuid-in-java) - [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path) - [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend) - [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue) @@ -111,4 +112,3 @@ - [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep) - [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator) - [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer) - diff --git a/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java b/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java new file mode 100644 index 0000000000..23baf5d5b4 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java @@ -0,0 +1,118 @@ +package com.baeldung.uuid; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.UUID; + +public class UUIDGenerator { + + /** + * These are predefined UUID for name spaces + */ + private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"; + private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"; + private static final String NAMESPACE_OID = "6ba7b812-9dad-11d1-80b4-00c04fd430c8"; + private static final String NAMESPACE_X500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8"; + + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); + + public static void main(String[] args) { + try { + System.out.println("Type 3 : " + generateType3UUID(NAMESPACE_DNS, "google.com")); + System.out.println("Type 4 : " + generateType4UUID()); + System.out.println("Type 5 : " + generateType5UUID(NAMESPACE_URL, "google.com")); + System.out.println("Unique key : " + generateUniqueKeysWithUUIDAndMessageDigest()); + } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + /** + * Type 4 UUID Generation + */ + public static UUID generateType4UUID() { + UUID uuid = UUID.randomUUID(); + return uuid; + } + + /** + * Type 3 UUID Generation + * + * @throws UnsupportedEncodingException + */ + public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException { + String source = namespace + name; + byte[] bytes = source.getBytes("UTF-8"); + UUID uuid = UUID.nameUUIDFromBytes(bytes); + return uuid; + } + + /** + * Type 5 UUID Generation + * + * @throws UnsupportedEncodingException + */ + public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException { + String source = namespace + name; + byte[] bytes = source.getBytes("UTF-8"); + UUID uuid = type5UUIDFromBytes(bytes); + return uuid; + } + + + public static UUID type5UUIDFromBytes(byte[] name) { + MessageDigest md; + try { + md = MessageDigest.getInstance("SHA-1"); + } catch (NoSuchAlgorithmException nsae) { + throw new InternalError("MD5 not supported", nsae); + } + byte[] bytes = md.digest(name); + bytes[6] &= 0x0f; /* clear version */ + bytes[6] |= 0x50; /* set to version 5 */ + bytes[8] &= 0x3f; /* clear variant */ + bytes[8] |= 0x80; /* set to IETF variant */ + return constructType5UUID(bytes); + } + + private static UUID constructType5UUID(byte[] data) { + long msb = 0; + long lsb = 0; + assert data.length == 16 : "data must be 16 bytes in length"; + + for (int i=0; i<8; i++) + msb = (msb << 8) | (data[i] & 0xff); + + for (int i=8; i<16; i++) + lsb = (lsb << 8) | (data[i] & 0xff); + return new UUID(msb, lsb); + } + + + /** + * Unique Keys Generation Using Message Digest and Type 4 UUID + * + * @throws NoSuchAlgorithmException + * @throws UnsupportedEncodingException + */ + public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException { + MessageDigest salt = MessageDigest.getInstance("SHA-256"); + salt.update(UUID.randomUUID() + .toString() + .getBytes("UTF-8")); + String digest = bytesToHex(salt.digest()); + return digest; + } + + public static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } + +}