Chance on generating 2 equal hashes based on time()?
Question is pretty simple, but I couldn't find an answer for this one... Basicly, my application is generating filenames with md5(time());
.
What are the chances, if any, that using this technique, I'll have 2 equal results?
P.S. Since my question title says hashes not exact hash, what are the chances, if any, again, of generating equal results for each type of开发者_如何学Python hashes sha1();
, sha512();
etc.?
Thanks in advance!
My estimation is it is unsafe due to possible changes in time by humans and other processes such as NTP which FrankH has kindly noted. I highly recommend using a cryptographically secure RNG (random number generator) if your framework allows.
Equal results are unlikely to result from this, you can simply validate that yourself by checking the uniqueness of md5(0)
... md5(INT32_MAX)
since that's the total range of a time_t
. I don't think there are collisions in that input space for any of the hashes you've named.
Predictable results is another matter, though. By choosing time()
as you input supplier, you restrict yourself to, well, one unique hash per second, no more than 86400 per day, ...
精彩评论