php - uniqid 有多独特?

标签 php

这个问题实际上并不是一个寻找解决方案的问题,它只是一个简单的好奇心问题。 PHP uniqid 函数有一个更熵标志,以使输出“更独特”。这让我想知道,当 more_entropy 为真时,这个函数产生相同结果的可能性有多大,而当它不是时。换句话说,启用 more_entropy 时 uniqid 与禁用时相比有多独特?一直启用 more_entropy 有什么缺点吗?

最佳答案

2014 年 3 月更新:

首先,需要注意的是,uniqid 有点用词不当,因为它不能保证唯一的 ID。

根据 PHP documentation :

WARNING!

This function does not create random nor unpredictable string. This function must not be used for security purposes. Use cryptographically secure random function/generator and cryptographically secure hash functions to create unpredictable secure ID.

This function 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().


根据文档,将 more-entropy 设置为 true 会生成更独特的值,但执行时间会更长(尽管程度很小):

If set to TRUE, uniqid() will add additional entropy (using the combined linear congruential generator) at the end of the return value, which increases the likelihood that the result will be unique.

注意行增加了结果唯一性的可能性,而不是保证唯一性。

您可以在一定程度上“无休止地”争取唯一性,并使用任意数量的加密例程进行增强,添加 salts等等 - 这取决于目的。

我建议查看关于 PHP 主要主题的评论,特别是:

http://www.php.net/manual/en/function.uniqid.php#96898

http://www.php.net/manual/en/function.uniqid.php#96549

http://www.php.net/manual/en/function.uniqid.php#95001

我建议弄清楚为什么你需要唯一性,是为了安全(即添加到加密/加扰例程中)?另外,它需要如何独一无二?最后,看看速度方面的考虑。适合性会随着基本考虑因素而改变。

关于php - uniqid 有多独特?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4070110/

相关文章:

php - 如何使用php在foreach循环中获取多维数组的键?

php - Zend 框架单元测试 : How to render response on redirect()

php - 如何使用javascript检查是否选择了文件?

php - Laravel 自定义验证消息作为数组导致错误

php - 将 PHP 添加到 jquery 函数

php - 是否有用于 Codeigniter 的 AJAX 库

php - php 中的 Pusher - 不要发送事件 [Unknown auth_key]

javascript - 使用 ng-click 发布表单(旧方式),这是不好的做法吗?

php - 如果条件匹配,数据库选择一个公共(public)字段

php - 与数据库中的多个日期和时间进行比较