Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中创建uniqe键值?_Php_Key_Uniqueidentifier - Fatal编程技术网

如何在PHP中创建uniqe键值?

如何在PHP中创建uniqe键值?,php,key,uniqueidentifier,Php,Key,Uniqueidentifier,如何在PHP中创建唯一的键值 我需要长度为20位(不超过33位)的简单唯一键。试试uniqid() 如果您可以使用pecl,您可以尝试此模块: 如果你在你的网络服务器上使用Ubuntu,还有一些东西:代码 $rand_val = md5(uniqid() + mt_rand()); Doc 免责声明 理论上有可能发生碰撞,但可能性不大。和的组合应该使它对于任何可能的用途都是不可用的。虽然这会产生一个32个字符长的字符串 (根据建设性意见编辑)使用uniqueid() 字符串uniqid(

如何在PHP中创建唯一的键值

我需要长度为20位(不超过33位)的简单唯一键。

试试uniqid()

如果您可以使用pecl,您可以尝试此模块:


如果你在你的网络服务器上使用Ubuntu,还有一些东西:

代码

$rand_val = md5(uniqid() + mt_rand());
Doc


免责声明
理论上有可能发生碰撞,但可能性不大。和的组合应该使它对于任何可能的用途都是不可用的。虽然这会产生一个32个字符长的字符串

(根据建设性意见编辑)

使用uniqueid()

字符串uniqid([string$prefix=”“[,bool$more\u entropy=false]]


虽然它是唯一的,但我不知道您为什么有最小长度要求。

您正在寻找加密密钥

不要在涉及安全性的任何上下文中使用
uniqid()
。在这种情况下使用它太容易预测。

该包允许您在PHP中生成加密强密钥。在本地PEAR安装中安装后,您可以使用它生成密钥,如下所示:

require_once 'Crypt/RSA.php';

// Creates a 96-bit key, which is 24 hex chars long
$key_pair = new Crypt_RSA_KeyPair(96);

//Returns public key from the pair
$public_key = $key_pair->getPublicKey();

//Returns private key from the pair
$private_key = $key_pair->getPrivateKey();
UUID对你的目标也有好处。下面是一个类,它将在所有系统上生成符合标准的UUID:

<?php

/**
 * UUID generator class
 *
 * Generates valid RFC 4211 compliant Universally Unique IDentifiers (UUID) version 3, 4 and 5. 
 * UUIDs generated validate using the OSSP UUID Tool, and the output for named-based UUIDs are 
 * exactly the same. This is a pure PHP implementation.
 *
 * Usage:
 * 
 *   Name-based UUID:
 *
 *     $v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
 *     $v5uuid = UUID::v5(UUID::NS_URL, 'http://www.google.com/');
 *
 *   Pseudo-random UUID:
 *
 *     $v4uuid = UUID::v4();
 *
 *
 * Originally found at: http://www.php.net/manual/en/function.uniqid.php#94959
 *
 * @author Andrew Moore 
 *
 *
 * Modifications made by Henry Merriam <php@henrymerriam.com> on 2009-12-20:
 *
 *   + Added constants for predefined namespaces as defined in RFC 4211 Appendix C.
 *     + NS_DNS
 *     + NS_URL
 *     + NS_ISO_UID
 *     + NS_X500_DN
 *
 *   + Wrote this documentation comment.
 *
 */
class UUID {

    const NS_DNS     = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // FQDN
    const NS_URL     = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; // URL
    const NS_ISO_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; // ISO OID
    const NS_X500_DN = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; // X.500 DN (in DER or a text output format)

    public static function v3($namespace, $name) {

        if(!self::is_valid($namespace)) return false;

        // Get hexadecimal components of namespace
        $nhex = str_replace(array('-','{','}'), '', $namespace);

        // Binary Value
        $nstr = '';

        // Convert Namespace UUID to bits
        for($i = 0; $i < strlen($nhex); $i+=2) {
            $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
        }

        // Calculate hash value
        $hash = md5($nstr . $name);

        // Format and return UUID
        return sprintf('%08s-%04s-%04x-%04x-%12s',

            // 32 bits for "time_low"
            substr($hash, 0, 8),

            // 16 bits for "time_mid"
            substr($hash, 8, 4),

            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 3
            (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,

            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

            // 48 bits for "node"
            substr($hash, 20, 12)
        );

    }

    public static function v4() {

        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

            // 32 bits for "time_low"
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),

            // 16 bits for "time_mid"
            mt_rand(0, 0xffff),

            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 4
            mt_rand(0, 0x0fff) | 0x4000,

            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            mt_rand(0, 0x3fff) | 0x8000,

            // 48 bits for "node"
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
        );

    }

    public static function v5($namespace, $name) {

        if(!self::is_valid($namespace)) return false;

        // Get hexadecimal components of namespace
        $nhex = str_replace(array('-','{','}'), '', $namespace);

        // Binary Value
        $nstr = '';

        // Convert Namespace UUID to bits
        for($i = 0; $i < strlen($nhex); $i+=2) {
            $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
        }

        // Calculate hash value
        $hash = sha1($nstr . $name);

        // Format and return UUID
        return sprintf('%08s-%04s-%04x-%04x-%12s',

            // 32 bits for "time_low"
            substr($hash, 0, 8),

            // 16 bits for "time_mid"
            substr($hash, 8, 4),

            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 5
            (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,

            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

            // 48 bits for "node"
            substr($hash, 20, 12)
        );

    }

    public static function is_valid($uuid) {
        return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
            '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
    }

}

这将为您创建一个至少20个字符长的唯一id

$id = '';
while(strlen($id) < 20)
{
   $id .= uniqid();
}
$id='';
而(斯特伦($id)<20)
{
$id.=uniqid();
}

我想使用一些默认编码,但所有默认编码都是针对比特数…请检查您的拼写。“创建”中没有“k”。@zneak为什么不将您的消息作为答案而不是评论发布?@David Caunt:因为这太微不足道了,我会为此而感到难过。UUID是他想要使用的,他可以在uniqid()评论中使用自定义函数。-1:uniqid(),虽然很独特,太过可预测,无法在安全上下文中使用。@Andrew,谁说过安全上下文?@Mike Sherov:问题中使用了术语key,与id相对。Key表示该值将用于安全目的。Key也可以是数据库键、memcache键或注册表键…不一定是安全相关的MD5有点限制,他不能确定它是否真正唯一,如果使用substr,这看起来像UUID的一个很差的复制品,这比你现在做的更好。-1:理论上的碰撞几率?极有可能。。。mt_rand()生成一个介于0和mt_getrandmax()之间的数字,在大多数系统上为2147483647。这意味着只有2147483647个可能的值。subsrt()是b/c,他清楚地说明了20-30个字符。同意这看起来不太好,但为什么不是32个字符呢?-1:uniqid()虽然唯一,但在安全上下文中使用起来太容易预测了。-1:uniqid()虽然唯一,但在安全上下文中使用起来太容易预测了。@Andrew Moore-这个问题从何说起安全上下文?@Mike Sherov:问题中使用了术语key,与id相对。Key表示该值将用于安全目的。我不是反对票,但谁说过安全上下文?@Mike Sherov:问题中使用了Key这个词,与id相对。Key表示该值将用于安全目的。-1:这不是获取唯一密钥的“简单”方法。这些要求简单而独特。@MyGGaN:这些要求是为了生成密钥,而不是id。这是两件不同的事情。id只是唯一标识对象的手段,没有真正的安全含义。密钥是用于安全目的的随机生成的秘密。那么PHP的array_keys()与安全性有什么关系?