There are two issues. One, does it contain special characters
(HTML/url markup, filesystem separator, etc? Most IDs are numeric or
restricted to Python "identifier" characters ([A-Za-z_-]), so while
you're checking it for validity you're simultaneously guaranteeing it
doesn't contain troublesome characters. Numeric IDs are especially
easy: 'if not id.isdigit(): abort(404. "Invalid ID")'.
The other issue is, does it matter if the public sees the ID? That
depends on the application. In two apps I have running, there's
nothing secret about the IDs; we don't mind if the user types the URL
for convenience rather than going through all the screens and clicks.
The only reason we don't promote it is users would get confused
("what's this meaningless number for? does it have meaning outside
your agency?") If the IDs were user-specific, such as bank account
numbers, then there may be a reason to disguise them.
--
Mike Orr <slugg...@gmail.com>
if you don't want to expose your id and create fake ids.
random.seed(id + SALT)
string_id ="%x" % random.randint(0, 0xffffffff)
and now you have an 8 chars id, with very few risks of collison. Thank
to Mersenne Twister a pretty good pseudo-random number generator
(http://docs.python.org/lib/module-random.html)
Then hex encoding waste a lot of place, because it's a very small alphabet.
0123456789abcdef
you can use a bigger one like base32, base 64 (see wikipedia and
replace "/" by something else like "_") or your own alphabet:
>>> import encode # function I've written, pretty trivial: while value > 0: value, rest = divmod(value, len(alphabet)); ...
>>> import random
>>> random.seed(1)
>>> x = random.randint(0, 0xffffffff)
>>> x
577090034
>>> "%x" % x
'2265b1f2'
>>> encode.encode(x, encode.BASE32)
'h6bcfi'
>>> encode.encode(x, encode.BASE64)
'ypr7O'
hex, base16 -> 8
base32 -> 7
base64 -> 6
...
I would say: avoid using MD5 as a random string generator because it
hasn't been created for that purpose.
Does anyone see any flaws with this, apart that the max is 2**32
items, and it's always bad to have a ceiling limit.
Cheers,
-- Yoan
If you want universally unique IDs and you have Python 2.5, see
http://docs.python.org/lib/module-uuid.html.
For my situation, normal autoincremented IDs are fine.
Happy Hacking!
-jj
--
It's a walled garden, but the flowers sure are lovely!
http://jjinux.blogspot.com/