Created
January 13, 2024 19:40
-
-
Save codegeek/c3ea46d1c3c623e336189c11635d7da8 to your computer and use it in GitHub Desktop.
Java class to map a UUID to a string and back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.nio.ByteBuffer; | |
import java.util.Base64; | |
import java.util.UUID; | |
public class UUIDToStringMapper { | |
public static final long LONG_MASK = 0xFFFFFFFFFFFFFFFFL; | |
private static final String CHARACTERS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; | |
private static final int KEY_LENGTH = 26; | |
public static String mapUUIDToString(UUID uuid) { | |
var combined = new BigInteger(1, ByteBuffer.allocate(16).putLong(uuid.getMostSignificantBits()) | |
.putLong(uuid.getLeastSignificantBits()).array()); | |
var key = new StringBuilder(KEY_LENGTH); | |
for (int i = 0; i < KEY_LENGTH; i++) { | |
var index = combined.and(BigInteger.valueOf(0x1fL)); | |
key.append(CHARACTERS.charAt(index.intValue())); | |
combined = combined.shiftRight(5); | |
} | |
key.reverse(); | |
return String.format("%s-%s-%s-%s", key.substring(0, 8), key.substring(8, 13), key.substring(13, 18), | |
key.substring(18)); | |
} | |
public static String mapUUIDToBase64(UUID uuid) { | |
return Base64.getUrlEncoder().withoutPadding().encodeToString(ByteBuffer.allocate(16).putLong(uuid | |
.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array()); | |
} | |
public static UUID mapStringToUUID(String key) { | |
key = key.replaceAll("-", "").toUpperCase(); | |
if (key.length() != KEY_LENGTH) { | |
throw new IllegalArgumentException("Invalid key length"); | |
} | |
var combined = BigInteger.ZERO; | |
for (int i = 0; i < KEY_LENGTH; i++) { | |
int index = CHARACTERS.indexOf(key.charAt(i)); | |
if (index == -1) { | |
throw new IllegalArgumentException("Invalid character in the key"); | |
} | |
combined = combined.shiftLeft(5).or(BigInteger.valueOf(index)); | |
} | |
return new UUID(combined.shiftRight(64).and(BigInteger.valueOf(LONG_MASK)).longValue(), | |
combined.and(BigInteger.valueOf(LONG_MASK)).longValue()); | |
} | |
public static UUID mapBase64ToUUID(String base64) { | |
var byteBuffer = ByteBuffer.wrap(Base64.getUrlDecoder().decode(base64)); | |
return new UUID(byteBuffer.getLong(), byteBuffer.getLong()); | |
} | |
public static void main(String[] args) { | |
var uuid = UUID.randomUUID(); | |
var mappedString = mapUUIDToString(uuid); | |
var mappedBase64 = mapUUIDToBase64(uuid); | |
System.out.printf("Original UUID: %s%n", uuid); | |
System.out.printf(" Without dash: %s%n", uuid.toString().replaceAll("-", "")); | |
System.out.printf("Mapped String: %s%n", mappedString); | |
System.out.printf(" Without dash: %s%n", mappedString.replaceAll("-", "")); | |
System.out.printf("Mapped base64: %s%n%n", mappedBase64); | |
System.out.printf("Reverted UUID from String: %s%n", mapStringToUUID(mappedString)); | |
System.out.printf("Reverted UUID from Base64: %s%n", mapBase64ToUUID(mappedBase64)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment