PHP5 effecient hash generation with minimal(no) chance of collision

南楼画角 提交于 2019-12-13 02:48:04

问题


Cryptology isn't my thing so hopefully there are a few better educated people here than me.

I would like store user data in memcache and I'd like to generate a unique to the user data. My original idea is to use the user's username, or another less descriptive piece of information, as the basis for some kind of hash.

My criteria is that the generation process will be

  • fast
  • produce the same value given the same input across different servers/environments/php versions
  • and have little to no chance of collisions (as this would be catastrophic) given a large number of inputs.

I'm not sure whether my usual weapons, sha1/2, would fit this criteria so I'm bowing to those better verse in these matters. Better safe than sorry.


回答1:


If your usernames are unique then why don't you use them directly rather than trying to generate some kind of hash?

  • They're already guaranteed to be unique.
  • They can be used as-is. No extra processing required.
  • They'd (probably, on average) be smaller than the corresponding SHA1 hash.



回答2:


SHA2(or even SHA1/MD5) does fit - if there'd be any chance of engineered (or even accidental) collisions, we wouldn't be using it. However, note that users may want to change their data, like zip code, or even name (getting married/divorced ...).

Therefore, a unique user handle or an automatically generated unique numeric ID are normally preferable.




回答3:


There does not seem to be anything wrong with sha1 for what you want. If collisions is a problem, according to wiki sha2 has not had any collisions yet.

your problem will be hashing a users name or something, as that will be duplicated and cause collisions. you should use the entire user row or something similar for the hash.

also the bigger / more complex the hash the longer it takes, generally.



来源:https://stackoverflow.com/questions/5028213/php5-effecient-hash-generation-with-minimalno-chance-of-collision

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!