Compress UUIDs





Compress UUIDs


Universally unique identifiers, or UUIDS are often used for database primary keys in scenarios where coordination of persistence is either impossible or impractical. A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.

In its canonical textual representation, the 16 octets of a UUID are represented as 32 hexadecimal (base-16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens). For example: 123e4567-e89b-12d3-a456-426614174000 xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

There are situations where you may want to pass a shorter string than 36 chars. I came across one such situation and started looking at compressing the uuid. Most of the compression schemes do not work out of the box. So I came up with a UUID specific compression scheme. I needed it in javascript so the coding below is in that language.


    function encode(webid){
           webid = webid.replace(/-/g, '');
           var str="";
           for (var i=0;i<webid.length/2;i++){
               var hex = webid.charAt(i*2) + webid.charAt(i*2+1);
               str += String.fromCharCode(parseInt(hex, 16));
           }
           return btoa(str);
       }
           
       var enc = encode('bacb37d0-a51f-491b-bd68-489c245b8c16');

The "-" in the UUID string are redundant as they are at specific location as prescribed above. Remove "-" as you can place them later. What remains is a hex numbers and that can be compressed into ascii of half the length. Now if you need to print this compress ascii array you have to convert it to printable characters. What better way then encoding it in base64 !


function decode(enc){
    var str = atob(enc);
    var dec="";
    for (var i=0;i<str.length;i++){
        dec_str = str.charCodeAt(i).toString(16);
        if (dec_str.length==1)dec_str = "0" + dec_str;
        dec+=dec_str;

    }
    // Add -
    dec = dec.slice(0,8) + "-" + dec.slice(8, 12) + "-" + dec.slice(12, 16) + "-" + dec.slice(16, 20) + "-" + dec.slice(20));
    
    return dec;
}

var dec = decode(enc);


To decode reverse the process and in the end add "-" at prescribed places. In above example bacb37d0-a51f-491b-bd68-489c245b8c16 (36 chars) is compressed to uss30KUfSRu9aEicJFuMFg== (24 chars) !