3

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?

Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169

2 Answers2

2

yes.

$filename = md5($_SERVER['REQUEST_URI'].$_SERVER['REMOTE_ADDR'].rand(50000000, 900000000000)).$ext;
genesis
  • 50,477
  • 20
  • 96
  • 125
  • 2
    Really? `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
  • 3
    This 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
  • 1
    I 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