md5(uniqid) makes sense for random unique tokens?

前端 未结 8 876
半阙折子戏
半阙折子戏 2020-12-12 17:12

I want to create a token generator that generates tokens that cannot be guessed by the user and that are still unique (to be used for password resets and confirmation codes)

相关标签:
8条回答
  • 2020-12-12 18:12

    To answer your question, the problem is you can't have a generator that is guaranteed random and unique as random by itself, i.e., md5(mt_rand()) can lead to duplicates. What you want is "random appearing" unique values. uniqid gives the unique id, rand() affixes a random number making it even harder to guess, md5 masks the result to make it yet even harder to guess. Nothing is unguessable. We just need to make it so hard that they wouldn't even want to try.

    0 讨论(0)
  • 2020-12-12 18:15

    This is a copy of another question I found that was asked a few months before this one. Here is a link to the question and my answer: https://stackoverflow.com/a/13733588/1698153.

    I do not agree with the accepted answer. According to PHPs own website "[uniqid] does not generate cryptographically secure tokens, in fact without being passed any additional parameters the return value is little different from microtime(). If you need to generate cryptographically secure tokens use openssl_random_pseudo_bytes()."

    I do not think the answer could be clearer than this, uniqid is not secure.

    0 讨论(0)
提交回复
热议问题