Generating a unique identifier

Paul Rubin http
Fri Sep 7 11:47:58 EDT 2007


Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
> def unique_id():
>    return os.urandom(10).encode('hex')

Sorry, make that 32 or 40 instead of 10, if the number of id's is large,
to make birthday collisions unlikely.

If you don't want the id's to be that large, you can implement a
Feistel cipher using md5 or sha as the round function pretty
straightforwardly, then just feed successive integers through it.
That also guarantees uniqueness, at least within one run of the
program.  I have some sample code around for that, let me know if you
need it.



More information about the Python-list mailing list