密码安全唯一ID

Sta*_*ros 13 php security uuid prng php-openssl

我想使用php生成加密安全的唯一uuids.

uniqid()提供唯一但不安全的id,openssl_random_pseudo_bytes()提供安全但不唯一的ID.两者(以下代码)的组合是正确的方法还是有更好的解决方案?

uniqid(bin2hex(openssl_random_pseudo_bytes(10)), true);
Run Code Online (Sandbox Code Playgroud)

Sco*_*ski 10

我想使用php生成加密安全的唯一uuids.

好的,这很容易做到.

uniqid()提供唯一但不安全的id,openssl_random_pseudo_bytes()提供安全但不唯一的ID.

是什么让你认为加密安全的伪随机数不是唯一的?

/**
 * Return a UUID (version 4) using random bytes
 * Note that version 4 follows the format:
 *     xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
 * where y is one of: [8, 9, A, B]
 * 
 * We use (random_bytes(1) & 0x0F) | 0x40 to force
 * the first character of hex value to always be 4
 * in the appropriate position.
 * 
 * For 4: http://3v4l.org/q2JN9
 * For Y: http://3v4l.org/EsGSU
 * For the whole shebang: https://3v4l.org/LNgJb
 * 
 * @ref https://stackoverflow.com/a/31460273/2224584
 * @ref https://paragonie.com/b/JvICXzh_jhLyt4y3
 * 
 * @return string
 */
function uuidv4()
{
    return implode('-', [
        bin2hex(random_bytes(4)),
        bin2hex(random_bytes(2)),
        bin2hex(chr((ord(random_bytes(1)) & 0x0F) | 0x40)) . bin2hex(random_bytes(1)),
        bin2hex(chr((ord(random_bytes(1)) & 0x3F) | 0x80)) . bin2hex(random_bytes(1)),
        bin2hex(random_bytes(6))
    ]);
}
Run Code Online (Sandbox Code Playgroud)

以上示例符合UUIDv4规范并使用PHP7的random_bytes()功能.

对于PHP 5个项目,你可以使用random_compat来填充工具random_bytes()从PHP 7.


dev*_*ull -1

为什么不对 openssl_random_pseudo_bytes 的输出进行哈希处理?您还可以连接时间戳并随后对其进行哈希处理

md5(bin2hex(openssl_random_pseudo_bytes(10)).strval(time()));
Run Code Online (Sandbox Code Playgroud)

仅以 md5 为例。您可以使用任何哈希算法。