UUIDs make great identifiers – ones that are, for most practical purposes, unique, easy to generate, and hard to guess. The only problem is that they are long – 256 bits, but with a textual representation that’s 36 characters. So what if they were shorter?
One way to make them shorter is to use fewer bytes. But that’s annoying (there’s lots of good tools to make 256-bit UUIDs, not so many to make, say, 128-bit ones), and defeats a lot of the benefits. Another way, however, is to use a different textual representation – particularly if you’re just using this as an identifier.
Enter Base64 encoding. By taking the binary representation of the UUID, and using Base64 encoding, you get a textual version that’s only 22 characters long, whilst still being able to read the resulting string. Base64 uses the 52 upper- and lower-case letters, plus the 10 digits, plus two other characters – normally / and +. That said, I recommend the use of ‘URL and Filename Safe Alphabet’, which uses _ and – instead. I’ve been using this to generate IDs for files, AWS resources and database keys for some time now.
To wrap this up, here’s some Java code to create these shorter UUIDs:
package biz.iesim.persistence.util; import java.nio.ByteBuffer; import java.util.Base64; import java.util.UUID; public class UUIDUtil { public static String shortUUID() { UUID uuid = UUID.randomUUID(); return shortUUID(uuid); } protected static String shortUUID(UUID uuid) { ByteBuffer byteBuffer = ByteBuffer.allocate(16); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); return Base64.getEncoder().withoutPadding().encodeToString(byteBuffer.array()) .replaceAll("/", "_") .replaceAll("\\+", "-"); } }
Share and Enjoy!
Thank you for your nice post. Is it possible to create a unique string from an UUID that is 24 characters long instead of 22 that is the length in your version?
Not without padding. A 256 byte UUID is 22 characters in base64 – that’s just what it maps to. However, you can add padding – and, strictly speaking, you should, as Base64 is meant to have multiples of 4 characters. To do so, just remove the ‘removePadding’ section in the code example above.