I was planning on hosting images on a server and wanted to use the same sort of file naming encryption mechanism. Is it just a hash?
Asked
Active
Viewed 3,743 times
2 Answers
2
yes.
$filename = md5($_SERVER['REQUEST_URI'].$_SERVER['REMOTE_ADDR'].rand(50000000, 900000000000)).$ext;

genesis
- 50,477
- 20
- 96
- 125
-
2Really? `md5` _and_ `sha1`? What you want to achieve with this? – KingCrunch Jun 22 '11 at 21:20
-
as much as possible unique hash. However it'S not required and I should remove it. – genesis Jun 22 '11 at 21:21
-
3This will cause collisions at some point. Best is to hash the internal primary key value identifying the resource, with some salting so that it's obviously not `sha(1)`, `sha(2)`, etc... – Marc B Jun 22 '11 at 21:30
-
@Marc B would it be too much to ask for an example? Thanks – Strong Like Bull Jun 22 '11 at 21:35
-
1@jini: assuming the image's id is in `$id`, `$hashed_id = sha1('some secret text to salt the id' . $id . 'some other secret salt text');` – Marc B Jun 22 '11 at 21:44
-
@Marc: problem is that 'some secret text to salt the id' is always the same and therefore it won't help. – genesis Jun 22 '11 at 21:47
-
1@genesis: doesn't have to be a cryptographically secure hash, just something so that the generated hash isn't going to be in any rainbow table. sha('1') is easy to get from any trivial checker. `sha1('some secret text1other secret text')` won't be. – Marc B Jun 22 '11 at 21:49
0
It's just a hash. If you have characters a-zA-Z0-9 and choose a hash only 6 characters long, you get 61,474,519 possible unique filenames. I doubt you'll run out =) use the mt_rand function for best results.

LainIwakura
- 2,871
- 2
- 19
- 22
-
1I think you mean that there are 56,800,235,584 possible unique 6-letter filenames, much more than what you quote. That's 62^6 possibilities with letters A-Za-z0-9. – Renaud Bompuis Dec 16 '13 at 06:10