我需要一个函数,可以生成唯一的字母数字令牌,我想使用uniqid,但它对于用户友好的令牌来说太长了。我需要从uniqid字符串的末尾取多少位才能缩短长度,同时我可以在一秒钟内进行1000个请求(每毫秒1个)。我知道uniqid使用microtime()来计算字符串。这是否可能,还是我必须使用其他函数?如果我必须使用另一个函数,那么你能否给我展示一个或两个函数?谢谢!
据看来,md4是最快的:http://www.php.net/manual/en/function.hash.php 但是生成32个字符。
crc32b 只有8个字符,并且仍然是最快的哈希之一。
echo hash('crc32b', 'http://stackoverflow.com');
这是luka8088发布的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
顺便说一下,delicious 曾经使用 MD5 对所有的 URI 进行加密(因此 delicious.com/md5hash 可以代表任何 URI)。
你尝试过类似这样的东西吗?
$string = md5(rand());
substr()
来缩短它。我发现这个方法非常快。md5(rand() . rand())
。 - Davidsubstr()
来缩短它。如果你想要20个字符,你可以这样做:substr(md5(rand()), 0, 20);
- David
openssl_random_pseudo_bytes
。 - Valerij:)
- halfer