Created
June 13, 2019 10:30
-
-
Save jamescookie/fcd8d230871b1a8ae1062478e0889bdc to your computer and use it in GitHub Desktop.
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.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