我应该如何创建我的GUID?

时间:2022-09-11 10:36:24

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?

我将要将图像上传到系统,并且需要通过非顺序唯一ID来引用它们。我已经阅读了一些关于GUID的内容,我想知道在PHP中创建一个GUID的最佳方法是什么。我应该md5()当前的时间戳和盐,或PHP的uniqueid(http://www.php.net/manual/en/function.uniqid.php)功能是否足够?

Thanks!

3 个解决方案

#1


20  

EDIT:

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.

哎呀!我忘记了我的这个古老的答案。为了澄清我天真所产生的混淆(与下面的评论一致):MD5(就像它们本质上最有用的哈希)并不是单射的,因此它们的输出并不能保证对所有输入都是唯一的。

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.

如果散列冲突是一个问题(在这种情况下,它们是),使用此技术将需要在散列后检查是否已生成相同的密钥。


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,因此几乎没有机会遇到过两次相同的时间。

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.

因此,如果您只是使用它来创建唯一的文件名,uniqid()就足够了。如果你想阻止用户猜测guid,你也可以把它变得更难和md5。

#2


6  

GUID is Microsoft's version of UUID. PHP's uniqid is version 4 of UUID. Definitely good enough.

GUID是Microsoft的UUID版本。 PHP的uniqid是UUID的第4版。绝对够好。

#3


2  

I also want to create guid for calling .net api and this function generate a key in guid format and it works for me

我也想创建用于调用.net api的guid,这个函数生成一个guid格式的密钥,它适用于我

function generateGuid($include_braces = false) {
    if (function_exists('com_create_guid')) {
        if ($include_braces === true) {
            return com_create_guid();
        } else {
            return substr(com_create_guid(), 1, 36);
        }
    } else {
        mt_srand((double) microtime() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));

        $guid = substr($charid,  0, 8) . '-' .
                substr($charid,  8, 4) . '-' .
                substr($charid, 12, 4) . '-' .
                substr($charid, 16, 4) . '-' .
                substr($charid, 20, 12);

        if ($include_braces) {
            $guid = '{' . $guid . '}';
        }

        return $guid;
    }
}

#1


20  

EDIT:

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.

哎呀!我忘记了我的这个古老的答案。为了澄清我天真所产生的混淆(与下面的评论一致):MD5(就像它们本质上最有用的哈希)并不是单射的,因此它们的输出并不能保证对所有输入都是唯一的。

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.

如果散列冲突是一个问题(在这种情况下,它们是),使用此技术将需要在散列后检查是否已生成相同的密钥。


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,因此几乎没有机会遇到过两次相同的时间。

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.

因此,如果您只是使用它来创建唯一的文件名,uniqid()就足够了。如果你想阻止用户猜测guid,你也可以把它变得更难和md5。

#2


6  

GUID is Microsoft's version of UUID. PHP's uniqid is version 4 of UUID. Definitely good enough.

GUID是Microsoft的UUID版本。 PHP的uniqid是UUID的第4版。绝对够好。

#3


2  

I also want to create guid for calling .net api and this function generate a key in guid format and it works for me

我也想创建用于调用.net api的guid,这个函数生成一个guid格式的密钥,它适用于我

function generateGuid($include_braces = false) {
    if (function_exists('com_create_guid')) {
        if ($include_braces === true) {
            return com_create_guid();
        } else {
            return substr(com_create_guid(), 1, 36);
        }
    } else {
        mt_srand((double) microtime() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));

        $guid = substr($charid,  0, 8) . '-' .
                substr($charid,  8, 4) . '-' .
                substr($charid, 12, 4) . '-' .
                substr($charid, 16, 4) . '-' .
                substr($charid, 20, 12);

        if ($include_braces) {
            $guid = '{' . $guid . '}';
        }

        return $guid;
    }
}