4

Possible Duplicate:
PHP short hash like URL-shortening websites

I need a function in which I can make url tokens with unique alpha-numeric tokens, I would use uniqid but it is too long for a user-likeable token, how much would I have to take off the end of a uniqid string so it is shorter but at the same time I could have 1000 requests in a second (so 1 per millisecond). I know that uniqid uses microtime() to calculate the string.

Is this even possible or would I have to use some other function. If I have to use another function then can you please show me one or two?

Thanks in advance!

Community
  • 1
  • 1
  • look into `openssl_random_pseudo_bytes` – Valerij Jul 28 '12 at 20:48
  • _How much would I have to take off the end so ... I could have 1000 requests in a second_ - there's too many variables to give a good answer to this. Depends on your server load and its CPU capacity, amongst other things. – halfer Jul 28 '12 at 20:49
  • @halfer can you please give me an answer suggesting something, I mean how many chars would I take off at the end without making it non-unique? –  Jul 28 '12 at 20:52
  • _how many chars would I take off at the end without making it non-unique_ - well, all hashes have collisions (the case of hashing two different things and getting the same result). The more you shorten, the more likely the chances of getting a collision. You need to trade that off with a length that is convenient for the user, as you say - so maybe 8 alpha-numeric characters (so 36^8)? Or, if you are prepared to use case sensitivity (62^8). That's still a lot of URLs - and you'd need a db look-up row for each. – halfer Jul 28 '12 at 21:10
  • @halfer would echo substr(uniqid(), 0, 10); be unique enough for what I am telling you? –  Jul 28 '12 at 21:16
  • That's 16^10 combinations. If you have less URLs than that, then yes! `:)` – halfer Jul 28 '12 at 21:19

2 Answers2

3

md4 seems to be the fastest: http://www.php.net/manual/en/function.hash.php but produces 32 characters

crc32b is 8 characters and still one of the fastest hashes

echo hash('crc32b', 'http://stackoverflow.com');

This is posted there by luka8088 as performance results on 1000 Kb :

Results: (in microseconds)
   1.  md4                           5307.912
   2.  md5                           6890.058
   3.  crc32b                        7298.946
   4.  crc32                         7561.922
   5.  sha1                          8886.098
   6.  tiger128,3                    11054.992
   7.  haval192,3                    11132.955
   8.  haval224,3                    11160.135
   9.  tiger160,3                    11162.996
  10.  haval160,3                    11242.151
  11.  haval256,3                    11327.981
  12.  tiger192,3                    11630.058
  13.  haval128,3                    11880.874
  14.  tiger192,4                    14776.945
  15.  tiger128,4                    14871.12
  16.  tiger160,4                    14946.937
  17.  haval160,4                    15661.954
  18.  haval192,4                    15717.029
  19.  haval256,4                    15759.944
  20.  adler32                       15796.184
  21.  haval128,4                    15887.022
  22.  haval224,4                    16047.954
  23.  ripemd256                     16245.126
  24.  haval160,5                    17818.927
  25.  haval128,5                    17887.115
  26.  haval224,5                    18085.002
  27.  haval192,5                    18135.07
  28.  haval256,5                    18678.903
  29.  sha256                        19020.08
  30.  ripemd128                     20671.844
  31.  ripemd160                     21853.923
  32.  ripemd320                     22425.889
  33.  sha384                        45102.119
  34.  sha512                        45655.965
  35.  gost                          57237.148
  36.  whirlpool                     64682.96
  37.  snefru                        80352.783
  38.  md2                           705397.844

p.s. delicious used to use MD5 for all URI's (so delicious.com/md5hash for any uri)

edelwater
  • 2,650
  • 8
  • 39
  • 67
  • possibly the outcome of the table on the bottom of http://en.wikipedia.org/wiki/Comparison_of_cryptographic_hash_functions – edelwater Jul 30 '12 at 11:59
0

Have you tried something like this?

$string = md5(rand()); 

If it's too long, you can just use substr() to shorten it. I've found it to be very fast.

David
  • 3,831
  • 2
  • 28
  • 38
  • It has been found, that hash('md5', 'string'); is faster than md5($string) (source md5 php page) – edelwater Jul 28 '12 at 20:59
  • I mean uniqness?? I appreciate ur help –  Jul 28 '12 at 21:07
  • MD5 is very unique - one character change in the input will cause a drastically different hash. If you want more uniqueness, you could use `md5(rand() . rand())` ad infinitum. – David Jul 28 '12 at 21:08
  • Again, you can use `substr()` to shorten it. If you want 20 characters, you do `substr(md5(rand()), 0, 20);` – David Jul 28 '12 at 21:15