Python - Why use anything other than uuid4() for unique strings?
Posted
by orokusaki
on Stack Overflow
See other posts from Stack Overflow
or by orokusaki
Published on 2010-03-12T18:35:41Z
Indexed on
2010/03/12
18:37 UTC
Read the original article
Hit count: 340
I see quit a few implementations of unique string generation for things like uploaded image names, session IDs, et al, and many of them employ the usage of hashes like SHA1, or others.
I'm not questioning the legitimacy of using custom methods like this, but rather just the reason. If I want a unique string, I just say this:
>>> import uuid
>>> uuid.uuid4()
07033084-5cfd-4812-90a4-e4d24ffb6e3d
And I'm done with it. I wasn't very trusting before I read up on uuid, so I did this:
>>> import uuid
>>> s = set()
>>> for i in range(5000000): # That's 5 million!
>>> s.add(uuid.uuid4())
...
...
>>> len(s)
5000000
Not one repeater (I didn't expect one considering the odds are like 1.108e+50, but it's comforting to see it in action). You could even half the odds by just making your string by combining 2 uuid4()
s.
So, with that said, why do people spend time on random() and other stuff for unique strings, etc? Is there an important security issue or other regarding uuid?
© Stack Overflow or respective owner