Encoding UUID to Base62 (not Base64) in Java

9.8k Views Asked by At

I'm trying to shorten UUID values (stored in DB as UUID, not string) to be embedded in URLs. I'm aware of Base64 for URL, but trying to see if I can make it without dash and underscore characters. So I would like to convert UUID to base62. After a lot of googling I found:

  1. There's not a standard for this (something like RFC2045), am I right?

  2. Most importantly there's no proper implementation for it. I found a lot of snippets on how to do it, but with some sort of note that "this is a naive implementation" or something. Is there a proper implementation (I don't mind the exact interpretation how the mapping should be done as long as it's done properly)?

  3. There are some base classes in Apache Commons Codec and Guava which are extended for Base32 and Base64, but I didn't find it easy to extend it for Base62. Is it even possible to do it (considering the fact that the mapping is fundamentally different)?
    Thanks.

2

There are 2 best solutions below

2
user3078523 On BEST ANSWER

You might want to try this library: https://github.com/Devskiller/friendly-id

The FriendlyID library converts a given UUID (with 36 characters) to a URL-friendly ID (a "FriendlyID") which is based on Base62 (with a maximum of 22 characters), as in the example below:

   UUID                                        Friendly ID
   
   c3587ec5-0976-497f-8374-61e0c2ea3da5   ->   5wbwf6yUxVBcr48AMbz9cb
   |                                           |                              
   36 characters                               22 characters or less

In addition, this library allows to:

  • convert from a FriendlyID back to the original UUID; and
  • create a new, random FriendlyID
0
fabiolimace On

You can encode UUIDs efficiently to base-62 using Base62Codec.

String string = Base62Codec.INSTANCE.encode(uuid);

You can also generate Friendly IDs with 22 characters or less.

public static String getFriendlyID() {
    return Base62Codec.INSTANCE.encode(UUID.randomUUID());
}

public static String getFriendlyID(int length) {
    return Base62Codec.INSTANCE.encode(UUID.randomUUID())
                      .substring(0, Math.min(length, 22));
}

Base62Codec is a codec of uuid-creator.

NOTE: the alphabet used by Base62Codec is "0-9A-Za-z", which is compatible with "C" collation (LC_COLLATE=C), which in turn makes libc sort strings in ASCII order, i.e., "AAA" is sorted before "aaa".


Disclaimer: I'm the main contributor of uuid-creator.