Encoding UUID to Base62 (not Base64) in Java

Viewed 6362

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 Answers

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

The codec Base62Codec can encode UUIDs efficiently to base-62.

// Returns a base-62 string
// uuid::: 01234567-89AB-4DEF-A123-456789ABCDEF
// base62: 0296tiiBY28FKCYq1PVSGd
String string = Base62Codec.INSTANCE.encode(uuid);

There are codecs for other encodings in the same package of uuid-creator.

Related