在 PHP 中生成 4 到 8 个字符的随机字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3954599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:33:20  来源:igfitidea点击:

Generate random string from 4 to 8 characters in PHP

phpstringhashrandomunique

提问by Jeremy Dicaire

I need to generate a string using PHP, it needto be uniqueand needto be from 4 to 8 characters (the value of a variable).

我需要使用 PHP 生成一个字符串,它需要唯一的,并且需要是 4 到 8 个字符(变量的值)。

I thought I can use crc32 hash but I can't decide how many characters, but sure it will be unique. In the other hand only create a "password generator" will generate duplicated string and checking the value in the table for each string will take a while.

我以为我可以使用 crc32 哈希,但我无法决定有多少个字符,但肯定它是唯一的。另一方面,只创建一个“密码生成器”就会生成重复的字符串,检查表中每个字符串的值需要一段时间。

How can I do that?

我怎样才能做到这一点?

Thanks!

谢谢!



Maybe I can use that :

也许我可以使用它:

  function unique_id(){
  $better_token = md5(uniqid(rand(), true));
  $unique_code = substr($better_token, 16);
  $uniqueid = $unique_code;
  return $uniqueid;
  }

  $id = unique_id();

Changing to :

更改为:

  function unique_id($l = 8){
  $better_token = md5(uniqid(rand(), true));
      $rem = strlen($better_token)-$l;
  $unique_code = substr($better_token, 0, -$rem);
  $uniqueid = $unique_code;
  return $uniqueid;
  }

  echo unique_id(4);

Do you think I'll get unique string each time for a goood while?

你认为我每次都会得到独特的字符串吗?

回答by Jeremy

In short, I think you'll get a pretty good random value. There's always the chance of a collision but you've done everything you can to get a random value. uniqid() returns a random value based on the current time in microseconds. Specifying rand() (mt_rand() would be better) and the second argument as true to uniqid() should make the value even more unique. Hashing the value using md5() should also make it pretty unique as even a small difference in two random values generated should be magnified by the hashing function. idealmachine is correct in that a longer value is less likely to have a collision than a shorter one.

简而言之,我认为你会得到一个很好的随机值。总是有碰撞的机会,但你已经尽你所能来获得一个随机值。uniqid() 根据当前时间(以微秒为单位)返回一个随机值。指定 rand() (mt_rand() 会更好)和第二个参数为真 uniqid() 应该使该值更加独特。使用 md5() 散列值也应该使它非常独特,因为即使生成的两个随机值的微小差异也应该被散列函数放大。Idealmachine 是正确的,因为较长的值比较短的值更不可能发生碰撞。

Your function could also be shorter since md5() will always return a 32 character long string. Try this:

您的函数也可以更短,因为 md5() 将始终返回 32 个字符的长字符串。尝试这个:

function unique_id($l = 8) {
    return substr(md5(uniqid(mt_rand(), true)), 0, $l);
}

回答by PleaseStand

The problem with randomness is that you can never be sure of anything. There is a small chance you could get one number this time and the same number the next. That said, you would want to make the string as long as possible to reduce that probability. As an example of how long such numbers can be, GUIDs (globally unique identifiers)are 16 bytes long.

随机性的问题在于您永远无法确定任何事情。您这次获得一个号码而下一次获得相同号码的可能性很小。也就是说,您可能希望使字符串尽可能长以降低这种可能性。作为此类数字的长度示例,GUID(全局唯一标识符)的长度为 16 个字节。

In theory, four hex characters (16 bits) give only 16^4 = 65536 possibilities, while eight hex characters (32 bits) give 16^8 = 4294967296. You, however, need to consider how likely it is for anytwo hashes to collide (the "birthday problem"). Wikipedia has a good tableon how likely such a collision is. In short, four hex characters are definitely not sufficient, and eight might not be.

理论上,四个十六进制字符(16 位)仅给出 16^4 = 65536 种可能性,而八个十六进制字符(32 位)给出 16^8 = 4294967296。但是,您需要考虑任意两个哈希值的可能性有多大碰撞(“生日问题”)。维基百科有一个很好的表格来说明这种碰撞的可能性。简而言之,四个十六进制字符绝对不够,八个可能不够。

You may want to consider using Base64 encoding rather than hex digits; that way, you can fit 48 bits in rather than just 32 bits.

您可能需要考虑使用 Base64 编码而不是十六进制数字;这样,您可以装入 48 位,而不仅仅是 32 位。

Eight bytes is 8 * 8 = 64 bits.

八个字节是 8 * 8 = 64 位。

回答by Daro from Poland

Reliable passwords You can only make from asciicharacters a-zA-Zand numbers 0-9. To do that best way is using only cryptographically secure methods, like random_int()or random_bytes()from PHP7. Rest functions as base64_encode()You can use only as support functions to make reliability of string and change it to ASCIIcharacters.

可靠的密码您只能使用ascii字符a-zA-Z数字 0-9。要做到这一点,最好的方法是仅使用加密安全的方法,例如PHP7 中的 random_int()random_bytes()。Rest 函数作为base64_encode()您只能用作支持函数来提高字符串的可靠性并将其更改为ASCII字符。

mt_rand()is not secure and is very old. From any string You must use random_int(). From binary string You should use base64_encode()to make binary string reliable or bin2hex, but then You will cut byte only to 16 positions (values). See my implementation of this functions.

mt_rand()不安全,而且很旧。从任何字符串您必须使用 random_int()。从二进制字符串你应该使用base64_encode()来使二进制字符串可靠或 bin2hex,但是你只会将字节剪切到 16 个位置(值)。 查看我对这个函数的实现。