3 ms·
Random file name: You can create an md5 hash of the original file name and use that for the public file name. From what I understand it's extremely unlikely to
by MichaelApproved 17y ago
Random file name: You can create an md5 hash of the original file name and use that for the public file name. From what I understand it's extremely unlikely to have two strings hash into the same value. It's also long and nearly impossible to guess.
Security: I had a similar need for my website and figured that if I'm the only one that knows about the URL then it's secure. I was dead wrong. Some browser plug-ins look at each url you enter and spider them. I know this is true because I started to see Alexa hit unpublished admin URLs on my website.
Unpublished URLs != Security.
- aarongough 17y agoI would second the idea of using a hash as the secret file name, however I would also recommend salting the hash with the current unix time. While having a collision between 2 different strings is extremely unlikely, having 2 users upload files with the same name is going to happen at some point. I would also recommend adding a salt to stop an attacker from being able to guess URLs by hashing common filenames. Though using the time in the hash is likely to prevent this, it's low-cost and probably worth it. So, I would recommend (in PHP): $url = md5( 'my_salt_string' . $time . $file_name ); But as Michael noted above, none of this can ever guarantee you security...
- mechanical_fish 17y agoPrecisely. URLs are not secrets. Browser plug-ins can leak them, they get logged in every proxy's log files, they show up on people's screens, they get emailed in the clear, they get bookmarked, they are easily sniffed...
- mooism2 17y agoOr create an md5 (or sha1) hash of the file. (Salted, unless you want people to use you as a rainbow table --- I would salt it with the content type.)
- cperciva 17y agoSalted, unless you want people to use you as a rainbow table --- I would salt it with the content type. Go read http://en.wikipedia.org/wiki/Salt_(cryptography) http://en.wikipedia.org/wiki/Salt_(cryptography) and come back once you understand what a salt is.
- deleted 17y ago[deleted]
- milestinsley 17y ago"Some browser plug-ins look at each url you enter and spider them" ... wow! I didn't know this. Thanks!
- andrewljohnson 17y agoIf you compute a string that is say 32 characters long using just letters and numbers, then there are 62^32 possible combinations. This number is 58 digits long. In this case, extremely unlikely just means never unless you are being attacked by say Russians with secret technology and a room full of supercomputers. The world will almost certainly end before anyone guesses one of these numbers or before you generate two that are the same. You can look at the probability table for the Birthday Attack in this Wikipedia article and decide how unlikely you would like this event to be: http://en.wikipedia.org/wiki/Birthday_problem http://en.wikipedia.org/wiki/Birthday_problem Also from the article: "In theory, MD5, 128 bits, should stay within that range until about 820 billion documents, even if its possible outputs are many more."
- tome 17y agoYou're also relying on the unpredictability of MD5 here, which is not so watertight as it was once believed to be: http://en.wikipedia.org/wiki/Md5#Vulnerability http://en.wikipedia.org/wiki/Md5#Vulnerability
- antirez 17y agoInstead to use the MD5 of the file name do this: create a random string and associate this string in your database. Simply grab 256 bits from /dev/urandom for instance (and convert this in hex). This has the very desirable property of not being able to guess the name of the file even if you know the name of the "object".
- mkyc 17y agoWhoa, no, don't do this. dsc-450.jpg > user/acbdddc1ab1b73536fabbd3f4ffeea4e dsc-450.jpgsalt > f043ce111eb398cd280b67c80c6c8ca6 usersaltdsc-450.jpg > 272ed08a85fc1e47b97462fb053a7c29 You'd need at least a secret site salt, as well as a per-user salt. If you have only a site salt, I can figure out what file names would be by uploading my own. If you had only a per-user salt, I calculate it myself. The problem of your site salt getting out remains. At the very least, you should randomly generate identifiers. This makes it less obvious which targets are worth looking at. What you really need, though, is something like per-folder access control.