问题描述
我将要上载图像到系统,并且需要使用非顺序唯一ID来引用它们.我已经阅读了一些有关GUID的文章,并且想知道用PHP制作GUID的最佳方法是什么.我应该将当前时间戳记为md5()并对其加盐,还是将PHP的唯一标识( http ://www.php.net/manual/zh/function.uniqid.php )功能是否足够?
I'm going to be uploading images to a system and need them to be referenced by a non-sequential unique ID. I've read a little about GUIDs, and I'm wondering what the best approach to making one in PHP is. Should I md5() the current timestamp and salt it, or will PHP's uniqueid (http://www.php.net/manual/en/function.uniqid.php) function be sufficient enough?
谢谢!
推荐答案
赞!我忘记了我这个古老的答案.为了澄清由我的幼稚造成的混乱(与下面的评论一致):MD5(就其性质而言,就像最有用的哈希一样)不是 内射的,因此不能保证它们的输出对于所有输入都是唯一的
Yikes! I forgot about this ancient answer of mine. To clarify confusion created by my naivety (consistent with the comments made below): MD5 (like most useful hashes, by their nature) are not injective, so their output is not guaranteed to be unique for all inputs.
如果哈希冲突是一个问题(在这种情况下是),则使用此技术将需要在哈希之后检查是否已经生成了相同的密钥.
If hash collisions are an issue (in this case, they are), using this technique will require checking, after hashing, whether an identical key has already been generated.
由于 uniqid 使用当前时间(以微秒为单位)来生成Guid,因此几乎没有机会碰到同一个人两次.
Since uniqid uses the current time in microseconds to generate the guid, there is virtually no chance you'll ever run into the same one twice.
因此,如果仅使用它来创建唯一的文件名,则uniqid()就足够了.如果要防止用户猜测该Guid,则最好使其更难,并对其进行md5处理.
So if you're just using it to make unique filenames, uniqid() will be sufficient. If you want to prevent users from guessing the guid, you might as well make it harder and md5 it as well.
这篇关于我应该如何创建我的GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!