The smallest builtin hash I am aware of is md5
>>> import hashlib
>>> hashlib.md5("hello worlds").digest().encode("base64")
'uWuHitcvVnCdu1Yo4c6hjQ==\n'
Low collision and short are somewhat mutually exclusive due to the birthday paradox
To make it urlsafe you need to use the function from the base64 module
>>> import base64
>>> base64.urlsafe_b64encode(hashlib.md5("hello world").digest())
'XrY7u-Ae7tCTyyK7j1rNww=='
However there should be no problem storing the 16 byte md5 digest in the database in binary form.
>>> md5bytes=hashlib.md5("hello world").digest()
>>> len(md5bytes)
16
>>> urllib.quote_plus(md5bytes)
'%5E%B6%3B%BB%E0%1E%EE%D0%93%CB%22%BB%8FZ%CD%C3'
>>> base64.urlsafe_b64encode(md5bytes)
'XrY7u-Ae7tCTyyK7j1rNww=='
You can choose either the quote_plus
or the urlsafe_b64encode
for your url, then decode with the corresponding function unquote_plus
or urlsafe_b64decode
before you look them up in the database.