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);
        byteBuffer.putLong(uuid.getMostSignificantBits());
        byteBuffer.putLong(uuid.getLeastSignificantBits());

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

Share and Enjoy!

Advertisements

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 18 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 16 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.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s