php - How to generate a unique key for an array? -
i'd generate unique identifier based on content of array. initial approach do:
$key = md5(json_encode($array));
however, i'd absolutely sure key unique , there remote possibility 2 distinct arrays can produce same md5 hash. current idea do:
$key = base64_encode(json_encode($array));
this guaranteed unique produces quite long key. can use sha512 or type of hash have same potential key collision md5? there way generate shorter key base64 method 100% guaranteed unique?
to 100% clear, question is: how can generate shortest possible 100% unique identifier set of data?
if want 100% guaranteed unique key match content, way use full length of content. can use json_encoded string as-is, or run through base64_encode() or bin2hex() or similar if want string doesn't have "special" characters. hash function md5, sha1, sha256 etc cannot 100% unique - because have fixed length, , due https://en.wikipedia.org/wiki/pigeonhole_principle there must non-unique results input content larger hash.
in practice, md5 , sha1 collisions have been published, stronger hash functions exist no collisions known or expected long time, using modern hash algorithm , safe not have duplicates.
Comments
Post a Comment