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.
Hey, Robert. Just a small observation about the sizes of currently standardized UUIDs. I think all implementations nowadays use 128 bits instead of 256. And I think the one you are using too, because to represent 256 bits using base64 you would actually need 43 bytes.
Nice, thanks. Except that UUIDs are 128 bit, not 256 bytes.
Lets say UUID = d50ae174-b65f-4ab9-b938-5f99b67ac50b which is 128 bits
If i shorten it using your shortUUID(), it will produce 1QrhdLZfSrm5OF-ZtnrFCw as the output.
When you convert this result (ASCII) to binary, this will become 176 bits
00110001010100010111001001101000011001000100110001011010011001100101001101110010011011010011010101001111010001100010110101011010011101000110111001110010010001100100001101110111
Memory wise, 176 bits is more compared to 128 bits though number of characters is less.
Old post but just wanted to clarify: UUID.randomUUID() is generating a 128 bit UUID, not 256 bits. Base64 generates a character for every 6 bits of data, so 128 bits would be 128/6 = 21.33 or 22 characters without padding. 256 bits would be 256/6 = 42.66 or 43 characters without padding.