code cleaning

This commit is contained in:
doljae 2022-04-25 10:51:40 +09:00
parent f5f4ea015e
commit c1cd68d30e
1 changed files with 50 additions and 66 deletions

View File

@ -1,6 +1,5 @@
package com.baeldung.uuid; package com.baeldung.uuid;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -10,49 +9,48 @@ import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
public class UUIDGenerator { public final class UUIDGenerator {
private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private UUIDGenerator() {}
/** /**
* Type 1 UUID Generation * Type 1 UUID Generation
*/ */
public static UUID generateType1UUID() { public static UUID generateType1UUID() {
long most64SigBits = get64MostSignificantBitsForVersion1(); final long most64SigBits = get64MostSignificantBitsForVersion1();
long least64SigBits = get64LeastSignificantBitsForVersion1(); final long least64SigBits = get64LeastSignificantBitsForVersion1();
return new UUID(most64SigBits, least64SigBits); return new UUID(most64SigBits, least64SigBits);
} }
private static long get64LeastSignificantBitsForVersion1() { private static long get64LeastSignificantBitsForVersion1() {
Random random = new Random(); final long random63BitLong = new Random().nextLong() & 0x3FFFFFFFFFFFFFFFL;
long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL; final long variant3BitFlag = 0x8000000000000000L;
long variant3BitFlag = 0x8000000000000000L;
return random63BitLong + variant3BitFlag; return random63BitLong + variant3BitFlag;
} }
private static long get64MostSignificantBitsForVersion1() { private static long get64MostSignificantBitsForVersion1() {
LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0); final LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
Duration duration = Duration.between(start, LocalDateTime.now()); final Duration duration = Duration.between(start, LocalDateTime.now());
long seconds = duration.getSeconds(); final long seconds = duration.getSeconds();
long nanos = duration.getNano(); final long nanos = duration.getNano();
long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100; final long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4; final long least12SignificantBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
long version = 1 << 12; final long version = 1 << 12;
return (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime; return (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificantBitOfTime;
} }
/** /**
* Type 3 UUID Generation * Type 3 UUID Generation
*
* @throws UnsupportedEncodingException
*/ */
public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException { public static UUID generateType3UUID(String namespace, String name) {
byte[] nameSpaceBytes = bytesFromUUID(namespace); final byte[] nameSpaceBytes = bytesFromUUID(namespace);
byte[] nameBytes = name.getBytes("UTF-8"); final byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
byte[] result = joinBytes(nameSpaceBytes, nameBytes); final byte[] result = joinBytes(nameSpaceBytes, nameBytes);
return UUID.nameUUIDFromBytes(result); return UUID.nameUUIDFromBytes(result);
} }
@ -61,32 +59,29 @@ public class UUIDGenerator {
* Type 4 UUID Generation * Type 4 UUID Generation
*/ */
public static UUID generateType4UUID() { public static UUID generateType4UUID() {
UUID uuid = UUID.randomUUID(); return UUID.randomUUID();
return uuid;
} }
/** /**
* Type 5 UUID Generation * Type 5 UUID Generation
*
* @throws UnsupportedEncodingException
*/ */
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException { public static UUID generateType5UUID(String namespace, String name) {
byte[] nameSpaceBytes = bytesFromUUID(namespace); final byte[] nameSpaceBytes = bytesFromUUID(namespace);
byte[] nameBytes = name.getBytes("UTF-8"); final byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
byte[] result = joinBytes(nameSpaceBytes, nameBytes); final byte[] result = joinBytes(nameSpaceBytes, nameBytes);
return type5UUIDFromBytes(result); return type5UUIDFromBytes(result);
} }
public static UUID type5UUIDFromBytes(byte[] name) { public static UUID type5UUIDFromBytes(byte[] name) {
MessageDigest md; final MessageDigest md;
try { try {
md = MessageDigest.getInstance("SHA-1"); md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException nsae) { } catch (NoSuchAlgorithmException exception) {
throw new InternalError("SHA-1 not supported", nsae); throw new InternalError("SHA-1 not supported", exception);
} }
byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16); final byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16);
bytes[6] &= 0x0f; /* clear version */ bytes[6] &= 0x0f; /* clear version */
bytes[6] |= 0x50; /* set to version 5 */ bytes[6] |= 0x50; /* set to version 5 */
bytes[8] &= 0x3f; /* clear variant */ bytes[8] &= 0x3f; /* clear variant */
@ -99,33 +94,28 @@ public class UUIDGenerator {
long lsb = 0; long lsb = 0;
assert data.length == 16 : "data must be 16 bytes in length"; assert data.length == 16 : "data must be 16 bytes in length";
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++) {msb = (msb << 8) | (data[i] & 0xff);}
msb = (msb << 8) | (data[i] & 0xff);
for (int i = 8; i < 16; i++) for (int i = 8; i < 16; i++) {lsb = (lsb << 8) | (data[i] & 0xff);}
lsb = (lsb << 8) | (data[i] & 0xff);
return new UUID(msb, lsb); return new UUID(msb, lsb);
} }
/** /**
* Unique Keys Generation Using Message Digest and Type 4 UUID * Unique Keys Generation Using Message Digest and Type 4 UUID
*
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/ */
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException { public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException {
MessageDigest salt = MessageDigest.getInstance("SHA-256"); final MessageDigest salt = MessageDigest.getInstance("SHA-256");
salt.update(UUID.randomUUID() salt.update(UUID.randomUUID()
.toString() .toString()
.getBytes("UTF-8")); .getBytes(StandardCharsets.UTF_8));
String digest = bytesToHex(salt.digest()); return bytesToHex(salt.digest());
return digest;
} }
private static String bytesToHex(byte[] bytes) { private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2]; final char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) { for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF; final int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; hexChars[j * 2 + 1] = hexArray[v & 0x0F];
} }
@ -133,36 +123,30 @@ public class UUIDGenerator {
} }
private static byte[] bytesFromUUID(String uuidHexString) { private static byte[] bytesFromUUID(String uuidHexString) {
String normalizedUUIDHexString = uuidHexString.replace("-", ""); final String normalizedUUIDHexString = uuidHexString.replace("-", "");
assert normalizedUUIDHexString.length() == 32; assert normalizedUUIDHexString.length() == 32;
byte[] bytes = new byte[16]; final byte[] bytes = new byte[16];
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
byte b = hexToByte(normalizedUUIDHexString.substring(i * 2, i * 2 + 2)); final byte b = hexToByte(normalizedUUIDHexString.substring(i * 2, i * 2 + 2));
bytes[i] = b; bytes[i] = b;
} }
return bytes; return bytes;
} }
public static byte hexToByte(String hexString) { public static byte hexToByte(String hexString) {
int firstDigit = Character.digit(hexString.charAt(0), 16); final int firstDigit = Character.digit(hexString.charAt(0), 16);
int secondDigit = Character.digit(hexString.charAt(1), 16); final int secondDigit = Character.digit(hexString.charAt(1), 16);
return (byte) ((firstDigit << 4) + secondDigit); return (byte) ((firstDigit << 4) + secondDigit);
} }
public static byte[] joinBytes(byte[] byteArray1, byte[] byteArray2) { public static byte[] joinBytes(byte[] byteArray1, byte[] byteArray2) {
int finalLength = byteArray1.length + byteArray2.length; final int finalLength = byteArray1.length + byteArray2.length;
byte[] result = new byte[finalLength]; final byte[] result = new byte[finalLength];
for (int i = 0; i < byteArray1.length; i++) {
result[i] = byteArray1[i];
}
for (int i = 0; i < byteArray2.length; i++) {
result[byteArray1.length + i] = byteArray2[i];
}
System.arraycopy(byteArray1, 0, result, 0, byteArray1.length);
System.arraycopy(byteArray2, 0, result, byteArray1.length, byteArray2.length);
return result; return result;
} }
@ -170,16 +154,16 @@ public class UUIDGenerator {
try { try {
byte[] bytes = name.getBytes(StandardCharsets.UTF_8); final byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
MessageDigest md = MessageDigest.getInstance("SHA-1"); final MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hash = md.digest(bytes); final byte[] hash = md.digest(bytes);
long msb = getLeastAndMostSignificantBitsVersion5(hash, 0); long msb = getLeastAndMostSignificantBitsVersion5(hash, 0);
long lsb = getLeastAndMostSignificantBitsVersion5(hash, 8); long lsb = getLeastAndMostSignificantBitsVersion5(hash, 8);
// Set the version field // Set the version field
msb &= ~(0xfL << 12); msb &= ~(0xfL << 12);
msb |= ((long) 5) << 12; msb |= 5L << 12;
// Set the variant field to 2 // Set the variant field to 2
lsb &= ~(0x3L << 62); lsb &= ~(0x3L << 62);
lsb |= 2L << 62; lsb |= 2L << 62;