[uuid] Added method to generate UUID identifiers for version 5 (#10948)

Signed-off-by: Diego Torres <dtorres.py@gmail.com>
This commit is contained in:
Diego Torres 2021-06-24 16:10:31 -04:00 committed by GitHub
parent 0c656299d0
commit f66bfa5088
2 changed files with 59 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package com.baeldung.uuid; package com.baeldung.uuid;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.time.Duration; import java.time.Duration;
@ -164,4 +166,44 @@ public class UUIDGenerator {
return result; return result;
} }
public static UUID generateType5UUID(String name) {
try {
byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hash = md.digest(bytes);
long msb = peekLong(hash, 0, ByteOrder.BIG_ENDIAN);
long lsb = peekLong(hash, 8, ByteOrder.BIG_ENDIAN);
// Set the version field
msb &= ~(0xfL << 12);
msb |= ((long) 5) << 12;
// Set the variant field to 2
lsb &= ~(0x3L << 62);
lsb |= 2L << 62;
return new UUID(msb, lsb);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
private static long peekLong(final byte[] src, final int offset, final ByteOrder order) {
long ans = 0;
if (order == ByteOrder.BIG_ENDIAN) {
for (int i = offset; i < offset + 8; i += 1) {
ans <<= 8;
ans |= src[i] & 0xffL;
}
} else {
for (int i = offset + 7; i >= offset; i -= 1) {
ans <<= 8;
ans |= src[i] & 0xffL;
}
}
return ans;
}
} }

View File

@ -61,4 +61,14 @@ class UUIDGeneratorUnitTest {
assertEquals(5, uuid.version()); assertEquals(5, uuid.version());
assertEquals(2, uuid.variant()); assertEquals(2, uuid.variant());
} }
@Test
public void version_5_UUID_is_correctly_generated_for_domain_baeldung_com_without_namespace() throws UnsupportedEncodingException {
UUID uuid = UUIDGenerator.generateType5UUID("baeldung.com");
assertEquals("a3c27ab0-2b46-55ef-b50e-0e5c57bfea94", uuid.toString());
assertEquals(5, uuid.version());
assertEquals(2, uuid.variant());
}
} }