Using shorter UUIDs

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);

        return Base64.getEncoder().withoutPadding().encodeToString(byteBuffer.array())
                .replaceAll("/", "_")
                .replaceAll("\\+", "-");

Share and Enjoy!

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

2 thoughts on “Using shorter UUIDs”

  1. 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?

    1. 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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s