Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

safe enough 8-character short unique random string

I am trying to compute 8-character short unique random filenames for, let's say, thousands of files without probable name collision. Is this method safe enough?

base64.urlsafe_b64encode(hashlib.md5(os.urandom(128)).digest())[:8] 

Edit

To be clearer, I am trying to achieve simplest possible obfuscation of filenames being uploaded to a storage.

I figured out that 8-character string, random enough, would be very efficient and simple way to store tens of thousands of files without probable collision, when implemented right. I don't need guaranteed uniqueness, only high-enough improbability of name collision (talking about only thousands of names).

Files are being stored in concurrent environment, so incrementing shared counter is achievable, but complicated. Storing counter in database would be inefficient.

I am also facing the fact that random() under some circumstances returns same pseudorandom sequences in different processes.

like image 413
zahory Avatar asked Nov 21 '12 00:11

zahory


People also ask

Is short UUID safe?

shortuuid is a simple python library that generates concise, unambiguous, URL-safe UUIDs. Often, one needs to use non-sequential IDs in places where users will see them, but the IDs must be as concise and easy to use as possible.

How do you generate unique strings in Python?

In order to generate random strings in Python, we use the string and random modules. The string module contains Ascii string constants in various text cases, digits, etc. The random module on the other hand is used to generate pseudo-random values.


1 Answers

Your current method should be safe enough, but you could also take a look into the uuid module. e.g.

import uuid  print str(uuid.uuid4())[:8] 

Output:

ef21b9ad 
like image 86
arshajii Avatar answered Sep 19 '22 15:09

arshajii