php - CakePHP中的唯一 token

标签 php security cakephp hash

在CakePHP中插入记录时,我需要创建真正唯一的 token 。该表可以包含数百万行,因此我不能仅基于一些随机生成的字符串。我也不想使用microtime(),因为尽管极少有机会在同一时刻提交两条记录,但可能性很小。
当然,最好的解决方案是使用String::uuid(),但是从cakephp文档开始

The uuid method is used to generate unique identifiers as per RFC 4122. The uuid is a 128bit string in the format of 485fc381-e790-47a3-9794-1337c0a8fe68.



因此,据我了解,它的代代币不使用蛋糕的安全盐。因此,我决定通过安全组件的哈希函数(或Auth Password函数)对它进行哈希处理,因为我需要它是唯一的并且同时非常非常安全。但是后来我发现了一个问题,说这不是一个好主意,但是对于php uniqid和md5。
Why is MD5'ing a UUID not a good idea?

而且,我也认为安全组件散列的字符串很难猜测-因为,例如for循环中的String::uuid()具有这样的输出
for ($i = 0; $i < 30; $i++) {
    echo String::uuid()."<br>";
}       
die;

// outputs
51f3dcda-c4fc-4141-aaaf-1378654d2d93
51f3dcda-d9b0-4c20-8d03-1378654d2d93
51f3dcda-e7c0-4ddf-b808-1378654d2d93
51f3dcda-f508-4482-852d-1378654d2d93
51f3dcda-01ec-4f24-83b1-1378654d2d93
51f3dcda-1060-49d2-adc0-1378654d2d93
51f3dcda-1da8-4cfe-abe4-1378654d2d93
51f3dcda-2af0-42f7-81a0-1378654d2d93
51f3dcda-3838-4879-b2c9-1378654d2d93
51f3dcda-451c-465a-a644-1378654d2d93
51f3dcda-5264-44b0-a883-1378654d2d93

因此,毕竟字符串的某些部分是相似的,但是在使用哈希函数的情况下,结果却大不相同
echo Security::hash('stackoverflow1');
echo "<br>";
echo Security::hash('stackoverflow2');

die;
// outputs
e9a3fcb74b9a03c7a7ab8731053ab9fe5d2fe6bd
b1f95bdbef28db16f8d4f912391c22310ba3c2c2

所以,问题是,我毕竟可以在Cake中哈希uuid()吗?或者,获得真正唯一和散列(根据我的安全盐更好)的安全 token 的最佳安全方法是什么。

更新

说安全 token ,是指猜测有多困难。 UUID确实是唯一的,但从上面的示例可以看出,某些记录具有某些相似性。但是散列结果却没有。

谢谢 !!

最佳答案

我认为您不必担心UUID重叠。

To put these numbers into perspective, the annual risk of someone being hit by a meteorite is estimated to be one chance in 17 billion,[38] which means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.



http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates

继续使用String::uuid()并轻松进行:)

关于php - CakePHP中的唯一 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17899091/

相关文章:

javascript - JQuery 无法处理 AJAX 请求

带有变量的 PHP 模板类?

php - 索引列但保持快速插入

php - CakePHP : onError method in AppModel is not called when there is a sql error

php - CakePHP 使用逗号分隔的 ids 连接

postgresql - 带有 PostgreSQL 的 CakePHP 2.2 新行插入失败 - 数据库错误 : Undefined table: 7 ERROR: relation "table_id_seq" does not exist

php - 如何使用php从数据库(MySql)显示不重复的随机图像

javascript - Google map Javascript API 的安全性

Java 小程序 java.security.AccessControlException

swift - 如何使用 FileManager 执行 Finder 等特权操作?