简体   繁体   English

PHP中的长随机字符串生成器

[英]Long random string generator in PHP

Does anyone know how to generate a long (eg 280 characters) random string in PHP without having to use a for loop that will loop through characters 280 times? 有没有人知道如何在PHP中生成一个长的(例如280个字符)随机string ,而不必使用将循环字符280次的for循环? I need it in order to create a custom session ID. 我需要它来创建自定义会话ID。

The PHPSESSID is not secure enough in my opinion being too short and not too random. 在我看来,PHPSESSID不够安全太短而且不太随机。 I know Facebook and Twitter, use long session IDs (150, 550 chars respectively). 我知道Facebook和Twitter,使用长会话ID(分别为150,550个字符)。

There could be an option to use MD5 strings or Bcrypt encryption of different string such as PHPSESSID , host, User-Agent etc. but I'm not sure this is the right way of doing it. 可以选择使用MD5字符串或不同字符串的Bcrypt加密,如PHPSESSID ,主机,用户代理等,但我不确定这是否正确。

If you're asking a question like that, it probably means you don't know anything about cryptography or security. 如果你问这样的问题,可能意味着你对密码学或安全性一无所知。 Trying to generate a "long random string" because, as you say, "The PHPSESSID is not secure enough" will probably lead you to a custom and insecure implementation. 试图生成一个“长随机字符串”,因为正如你所说,“PHPSESSID不够安全”可能会引导你进行自定义和不安全的实现。

Generating a random string is IMPOSSIBLE , at least not with your current hardware: you may approximate a fair pseudorandom generator but that is only useful for educational purposes. 生成随机字符串是不可能的 ,至少不是您当前的硬件:您可以近似一个公平的伪随机生成器,但这仅用于教育目的。

PHP's Session ID generation algorithm is fairly efficient; PHP的会话ID生成算法相当有效; if you think it is not secure enough, then you'll likely waste time making it better. 如果你认为它不够安全,那么你可能会浪费时间让它变得更好。 You may probably want to use a different authentication mechanism if you are looking at maximum security (using a client certificate for example). 如果您正在考虑最高安全性(例如,使用客户端证书),您可能希望使用不同的身份验证机制。

If websites such as Twitter, Facebook, or another site with similar traffic use longer session IDs, it may be not because it is more secure (well in a way), but rather because it avoids conflicts. 如果Twitter,Facebook或具有类似流量的其他网站等网站使用较长的会话ID,则可能不是因为它更安全(在某种程度上),而是因为它避免了冲突。

Finally, if you want a longer session ID without trying to write your own algorithm, you should use the following PHP configuration directive: 最后,如果您想要更长的会话ID而不尝试编写自己的算法,则应使用以下PHP配置指令:

session.hash_function which can take any hash algorithm known by PHP. session.hash_function ,它可以采用PHP已知的任何哈希算法。

You may also want to use session.bits_per_characters to shorten or lengthen the string. 您可能还希望使用session.bits_per_characters来缩短或延长字符串。 Note that if you do this, the string may be longer or shorter, but the data remains the same -- only represented differently (base 16, base 32, etc.) 请注意,如果这样做,字符串可能会更长或更短,但数据保持不变 - 仅表示不同(基数为16,基数为32等)

Additional info: 附加信息:

You may also increase the entropy by using a custom source (file) and setting the length of the seed: 您还可以通过使用自定义源(文件)并设置种子长度来增加熵:

ini_set("session.hash_function", "sha512");
ini_set("session.bits_per_charater", 4); // 4 means hex
ini_set("session.entropy_file", "/dev/urandom");
ini_set("session.entropy_length", "512");

Can it be binary hexadecimal? 它可以是 二进制 十六进制吗?

bin2hex(mcrypt_create_iv(140, MCRYPT_DEV_URANDOM));

Or if you have access to hash_pbkdf2() : 或者,如果您有权访问hash_pbkdf2()

hash_pbkdf2('sha256', PHPSESSID, mt_rand(), 1, 280, false);

You'll probably find that not all the bits in a "long" session id are purely random - they might provide additional information to the application to enable rapid extraction of key information. 您可能会发现并非“长”会话ID中的所有位都是纯随机的 - 它们可能会为应用程序提供其他信息,以便快速提取关键信息。

PHP session ids by default aren't too bad, and can be improved by providing some extra entropy (eg from /dev/urandom) PHP会话ID默认情况下也不错,可以通过提供一些额外的熵来改进(例如来自/ dev / urandom)

Look at php_session_create_id in ext/session/session.c in the php source 查看php源代码中ext / session / session.c中的php_session_create_id

It goes like this: 它是这样的:

  • get time of day 得到时间
  • get remote ip address 获取远程IP地址
  • build a string with the seconds and microseconds from the current time, along with the IP address 使用当前时间的秒和微秒以及IP地址构建一个字符串
  • feed that into configured session hash function (either MD5 or SHA1) 将其提供给配置的会话哈希函数 (MD5或SHA1)
  • if configured, feed some additional randomness from an entropy file 如果已配置,则从熵文件中提供一些额外的随机性
  • generate final hash value 生成最终哈希值

So getting a duplicate or predicting a session id is pretty difficult. 因此,获取重复或预测会话ID非常困难。

Make yourself a long string and shuffle it. 让自己成长串并将其洗牌

$string = 'asdfhdfiadfshsdfDSGFADSFDSFDSFER524353452345';
$new_string = shuffle($string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM