PHP5 effecient hash generation with minimal(no) chance of collision
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.
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.
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.
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.
精彩评论