Explain Codes LogoExplain Codes Logo

Storing UUID as base64 String

java
base64-encoding
uuid
java-8
Nikita BarsukovbyNikita Barsukov·Jan 10, 2025
TLDR

Let's quickly encode UUIDs as base64 for compact SQL storage. We achieve this by converting the UUID to binary and then to base64. Here's a SQL snippet showcasing the action:

DECLARE @uuid_base64 VARCHAR(24) = CAST('' AS XML).value('xs:base64Binary(CAST(CAST(@uuid AS UNIQUEIDENTIFIER) AS VARBINARY(16)))', 'VARCHAR(24)'); SELECT @uuid_base64;

This conversion significantly reduces the storage size and delivers a readable and efficiently retrieveable UUID format.

Breaking down base64 encoding

Why base64 for UUIDs?

Base64 encoding shrinks a UUID into a more manageable string, preserving its uniqueness and readability. The transformation is considerable, standard UUID strings have a 36 character length (comprised of 32 hexadecimal characters and 4 hyphens), whereas the base64 representation needs only 22 characters sans padding.

Encoding a UUID

Start by transforming the UUID into a byte array. Leveraging Java's UUID class together with the ByteBuffer wrapper simplifies this:

UUID uuid = UUID.randomUUID(); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.putLong(uuid.getMostSignificantBits()); // Packing the most significant bits, just like packing the most important socks. byteBuffer.putLong(uuid.getLeastSignificantBits()); // Can't forget the less significant bits, it's like the toothbrush! byte[] uuidBytes = byteBuffer.array();

With a byte array in tow, the base64 encoding ensues:

String encodedUUID = Base64.getUrlEncoder().withoutPadding().encodeToString(uuidBytes); // Et voila! Compact and travel-friendly

Back to UUID

Comfort in knowing that the base64 string can be trustworthy converted back into the original UUID is essential. Decode the string and employ ByteBuffer to rebuild the UUID:

byte[] decodedBytes = Base64.getUrlEncoder().withoutPadding().decode(encodedUUID); ByteBuffer byteBuffer = ByteBuffer.wrap(decodedBytes); UUID originalUUID = new UUID(byteBuffer.getLong(), byteBuffer.getLong()); // Presto! Now you see me, now you don't!

Maintaining data integrity mandates the verification of the reconverted base64 string against the original UUID.

Encoder's insight

Other encoding formats: base85

While base64 is compact, base85 takes it a notch higher regarding density, cramming four bytes of binary data into a whimsy of five characters.

URL safety and padding

Base64 encoded strings can harbor +/ which could become problematic in URLs. Opting for URL-safe base64 encoding circumvents these characters that would otherwise need URI escaping. Stripping off the conventional == padding minutely saves space.

Handling across different languages

Diverse programming languages have unique peculiarities when grappling with encoding. Case in point, Ruby’s shift operations for encoding differ markedly from Java counterparts.

Intricacies and caution

Length validation

To ensure proper encoding sans padding, it's imperative to enforce a 22 characters rule on your base64 encoded UUID.

Language-specific implementations

In tailoring this approach to your applications, be keen on the nuances and libraries of your chosen programming language. Develop compatibility checks and ensure a fallback mechanism for libraries built around padded base64 strings.

Trading off factors

While the base64 representation of a UUID entails storage space savings, considerations such as human readability and the processing overhead of encoding/decoding should influence your implementation choice.

Seeking expert review

Always leverage peer or expert critique when implementing base64 encoding, it can help identify potential implementation snags and validate the encoding's correctness and efficiency.