这是在PHP中创建加密惟一令牌的最安全的方法


Which is the most secure method of creating a cryptographically unique token in PHP

我有这个工作,我只是想要一些关于创建令牌(用于API)的最安全选项的建议,他们中的任何一个都会有任何问题向前发展吗?

选项:

$token = bin2hex(openssl_random_pseudo_bytes(16));

或选项B:

// $username - unique username for this user
$token = hash('sha512', sha1($username.time().rand(1000, 9999)));

虽然这个问题离题了,但很有趣。

我的答案:选项a。

选项B至少包含一个可预测的值$username。time()的值可能是可预测的,如果种子可以猜出,rand()的值也是可预测的。

看看这个:https://crackstation.net/hashing-security.htm

编辑补充:当然,如果令牌不需要保密,它没有任何区别。您可以直接使用用户名