Launcher/compat/auth/uuid/ReversibleUUIDs.java

20 lines
627 B
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
public static UUID toUUID(String username) {
ByteBuffer buffer = ByteBuffer.wrap(Arrays.copyOf(username.getBytes(StandardCharsets.US_ASCII), 16));
return new UUID(buffer.getLong(), buffer.getLong()); // MOST, LEAST
}
public static String toUsername(UUID uuid) {
byte[] bytes = ByteBuffer.allocate(16).
putLong(uuid.getMostSignificantBits()).
putLong(uuid.getLeastSignificantBits()).array();
// Find username end
int length = 0;
while(length < bytes.length && bytes[length] != 0) {
length++;
}
// Decode and verify
return VerifyHelper.verifyUsername(new String(bytes, 0, length, StandardCharsets.US_ASCII));
}