diff --git a/core-java-modules/core-java-uuid/pom.xml b/core-java-modules/core-java-uuid/pom.xml
index e6710dbdf2..76154033c2 100644
--- a/core-java-modules/core-java-uuid/pom.xml
+++ b/core-java-modules/core-java-uuid/pom.xml
@@ -29,6 +29,16 @@
tsid-creator
5.2.3
+
+ commons-codec
+ commons-codec
+ 1.16.0
+
+
+ org.apache.commons
+ commons-lang3
+ 3.14.0
+
diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/DecodeUUIDStringFromBase64Test.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/DecodeUUIDStringFromBase64Test.java
new file mode 100644
index 0000000000..043ec59fb5
--- /dev/null
+++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/DecodeUUIDStringFromBase64Test.java
@@ -0,0 +1,61 @@
+package com.baeldung.uuid;
+
+import static org.apache.commons.codec.binary.Base64.decodeBase64;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.Base64;
+import java.util.UUID;
+
+import org.apache.commons.lang3.Conversion;
+import org.junit.jupiter.api.Test;
+
+public class DecodeUUIDStringFromBase64Test {
+ private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
+
+ @Test
+ public void shouldDecodeUUIDUsingByteArrayAndBase64Decoder() {
+ byte[] decodedBytes = Base64.getDecoder()
+ .decode("UUrxjPeTX8xsDDoxQOfGgw");
+ UUID uuid = convertToUUID(decodedBytes);
+ assertEquals(originalUUID, uuid);
+ }
+
+ @Test
+ public void shouldDecodeUUIDUsingByteBufferAndBase64UrlDecoder() {
+ byte[] decodedBytes = Base64.getUrlDecoder()
+ .decode("zF-T94zxSlGDxudAMToMbA");
+ ByteBuffer byteBuffer = ByteBuffer.wrap(decodedBytes);
+ long mostSignificantBits = byteBuffer.getLong();
+ long leastSignificantBits = byteBuffer.getLong();
+ UUID uuid = new UUID(mostSignificantBits, leastSignificantBits);
+ assertEquals(originalUUID, uuid);
+ }
+
+ @Test
+ public void shouldDecodeUUIDUsingApacheUtils() {
+ byte[] decodedBytes = decodeBase64("UUrxjPeTX8xsDDoxQOfGgw");
+ UUID uuid = Conversion.byteArrayToUuid(decodedBytes, 0);
+ assertEquals(originalUUID, uuid);
+ }
+
+ private UUID convertToUUID(byte[] src) {
+ long mostSignificantBits = convertBytesToLong(src, 0);
+ long leastSignificantBits = convertBytesToLong(src, 8);
+
+ return new UUID(mostSignificantBits, leastSignificantBits);
+ }
+
+ private long convertBytesToLong(byte[] uuidBytes, int start) {
+ long result = 0;
+
+ for(int i = 0; i < 8; i++) {
+ int shift = i * 8;
+ long bits = (255L & (long)uuidBytes[i + start]) << shift;
+ long mask = 255L << shift;
+ result = result & ~mask | bits;
+ }
+
+ return result;
+ }
+}
diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/EncodeUUIDToBase64StringTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/EncodeUUIDToBase64StringTest.java
new file mode 100644
index 0000000000..cef80cdc24
--- /dev/null
+++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/EncodeUUIDToBase64StringTest.java
@@ -0,0 +1,59 @@
+package com.baeldung.uuid;
+
+import static org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.Base64;
+import java.util.UUID;
+
+import org.apache.commons.lang3.Conversion;
+import org.junit.jupiter.api.Test;
+
+public class EncodeUUIDToBase64StringTest {
+ private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
+
+ @Test
+ public void shouldEncodeUUIDUsingByteArrayAndBase64Encoder() {
+ byte[] uuidBytes = convertToByteArray(originalUUID);
+ String encodedUUID = Base64.getEncoder().withoutPadding()
+ .encodeToString(uuidBytes);
+ assertEquals("UUrxjPeTX8xsDDoxQOfGgw", encodedUUID);
+ }
+
+ @Test
+ public void shouldEncodeUUIDUsingByteBufferAndBase64UrlEncoder() {
+ ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
+ byteBuffer.putLong(originalUUID.getMostSignificantBits());
+ byteBuffer.putLong(originalUUID.getLeastSignificantBits());
+ String encodedUUID = Base64.getUrlEncoder().withoutPadding()
+ .encodeToString(byteBuffer.array());
+ assertEquals("zF-T94zxSlGDxudAMToMbA", encodedUUID);
+ }
+
+ @Test
+ public void shouldEncodeUUIDUsingApacheUtils() {
+ byte[] bytes = Conversion.uuidToByteArray(originalUUID, new byte[16], 0, 16);
+ String encodedUUID = encodeBase64URLSafeString(bytes);
+ assertEquals("UUrxjPeTX8xsDDoxQOfGgw", encodedUUID);
+ }
+
+ private byte[] convertToByteArray(UUID uuid) {
+ byte[] result = new byte[16];
+
+ long mostSignificantBits = uuid.getMostSignificantBits();
+ fillByteArray(0, 8, result, mostSignificantBits);
+
+ long leastSignificantBits = uuid.getLeastSignificantBits();
+ fillByteArray(8, 16, result, leastSignificantBits);
+
+ return result;
+ }
+
+ private static void fillByteArray(int start, int end, byte[] result, long bits) {
+ for (int i = start; i < end; i++) {
+ int shift = i * 8;
+ result[i] = (byte) ((int) (255L & bits >> shift));
+ }
+ }
+}