1

I have a database of entities that I want to display in a web ui. I want a short, alphanumeric string that I can generate from the entity's key. I came to the conclusion that base32 was a good solution (particularly because I wanted the keys to be case-insensitive so they could be read verbally, etc). Is there anything shorter or more space efficient than the below?

import base64
def b32urlencode(x):
    return base64.b32encode(x).strip('=').lower()

def b32urldecode(x):
    return base64.b32decode(x + ('=' * (8 - (len(x) % 8))))
Aaron
  • 2,341
  • 21
  • 27
  • 1
    Why Base32 and not Base64? The latter is more compact and only needs a minor tweak to be a valid URL slug. – Martijn Pieters Aug 28 '13 at 17:03
  • 1
    See [Convert UUID 32-character hex string into a "YouTube-style" short id and back](http://stackoverflow.com/a/12270917) for the tweak in question. – Martijn Pieters Aug 28 '13 at 17:04
  • 1
    This is very interesting (I'm looking at it now). I chose base32 because I wanted it to be case-insensitive (in case it ever needs to be read over the phone, for example). – Aaron Aug 28 '13 at 17:14
  • Right, that is a valid constraint, but you should include that in your post then. :-) – Martijn Pieters Aug 28 '13 at 17:14
  • Thank you Martijn, updated the post now :) Your YouTube-style solution is very interesting, and I appreciate you sharing it. I'm going to look at the pros & cons for my specific solution. – Aaron Aug 28 '13 at 17:39

1 Answers1

1

The answer depends on how human-readable and -typeable you want the URL to be.

Base64 uses upper- and lower-case letters plus -, +, _, / and =. Upper and lower case may or may not be distinguishable by your web server or application, depending on how you've configured them. If you expect your users to be able type URLs (not just click on them) then upper and lower case alphabetics can be a problem even if the server & app tolerate them.

Depending on the above, consider base 62 (a-z, A-Z and 0-9) or 36 (a-z case insensitive and 0-9).

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80