Skip to content

Instantly share code, notes, and snippets.

@jamescookie
Created June 13, 2019 10:30
Show Gist options
  • Save jamescookie/fcd8d230871b1a8ae1062478e0889bdc to your computer and use it in GitHub Desktop.
Save jamescookie/fcd8d230871b1a8ae1062478e0889bdc to your computer and use it in GitHub Desktop.
import java.math.BigInteger;
import java.util.Collections;
import java.util.UUID;
import java.util.stream.Stream;
public class ShorterUuid {
private static void doit() {
String original = UUID.randomUUID().toString();
String hex = original.replaceAll("-", "");
String s = new BigInteger(hex, 16).toString(36);
String unpadded = new BigInteger(s, 36).toString(16);
StringBuilder padded = new StringBuilder(String.join("", Collections.nCopies(32, "0")).substring(unpadded.length()) + unpadded);
Stream.of(20, 16, 12, 8).forEach(index -> padded.insert(index, "-"));
assert original.equals(padded.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment