Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL-Compact representation of GUID/UUID?

Tags:

I need to generate a GUID and save it via a string representation. The string representation should be as short as possible as it will be used as part of an already-long URL string.

Right now, instead of using the normal abcd-efgh-... representation, I use the raw bytes generated and base64-encode them instead, which results in a somewhat shorter string.

But is it possible to make it even shorter?

I'm OK with losing some degree of uniqueness and keeping a counter, but scanning all existing keys is not an option. Suggestions?

like image 241
chakrit Avatar asked May 24 '10 19:05

chakrit


People also ask

Can I use UUID in URL?

Yes. A UUID consists of only hexadecimal characters (a–f, 0–9) plus a hyphen (-). As per RFC 3986 (URI Syntax) §2.3, hyphen and hexadecimal characters are included in those explicitly unreserved: Characters that are allowed in a URI but do not have a reserved purpose are called unreserved.

Is GUID same as UUID?

The GUID designation is an industry standard defined by Microsoft to provide a reference number which is unique in any context. UUID is a term that stands for Universal Unique Identifier. Similarly, GUID stands for Globally Unique Identifier. So basically, two terms for the same thing.

What is the format of a GUID?

The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). Note that utilities such as GUIDGEN can generate GUIDs containing lowercase letters.


2 Answers

I used an Ascii85 encoding for writing a Guid to a database column in 20 ASCII characters. I've posted the C# code in case it is useful. The specific character set may be different for a URL encoding, but you can pick whichever characters suit your application. It's available here: What is the most efficient way to encode an arbitrary GUID into readable ASCII (33-127)?

like image 157
sheikhjabootie Avatar answered Sep 20 '22 15:09

sheikhjabootie


Sure, just use a base larger than 64. You'll have to encode them using a custom alphabet, but you should be able to find a few more "url-safe" printable ASCII characters.

Base64 encodes 6 bits using 8, so a 16 byte GUID value becomes 22 bytes encoded. You may be able to reduce that by a character or two, but not much more.

like image 31
Greg Hewgill Avatar answered Sep 19 '22 15:09

Greg Hewgill