久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 PHP 中生成隨機密鑰的最佳方法是什么?

What is the best way to generate a random key within PHP?(在 PHP 中生成隨機密鑰的最佳方法是什么?)
本文介紹了在 PHP 中生成隨機密鑰的最佳方法是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望創建一個可重復使用的函數,該函數將生成一個隨機密鑰,該密鑰具有選定長度的可打印 ACSII 字符(從 2 到 1000+ 的任意位置).我認為可打印的 ASCII 字符是 33-126.它們的密鑰不需要完全唯一,只要在完全相同的毫秒內生成就可以是唯一的(因此 uniqid() 將不起作用).

I'm looking to create a reusable function that will generate a random key with printable ACSII characters of chosen length (anywhere from 2 to 1000+). I'm thinking printable ASCII characters would be 33-126. They key does not need to be completely unique, just unique if generated at the exact same millisecond (so uniqid() won't work).

我認為 chr()mt_rand() 的組合可能會起作用.

I'm thinking a combination of chr() and mt_rand() might work.

這是要走的路,還是其他最好的方法?

Is this the way to go, or is something else the best method?

uniqid() 也不會工作,因為它沒有長度參數,它只是 PHP 給你的.

uniqid() will also not work because it doesn't have a length parameter, it's just whatever PHP gives you.

我的想法:這就是我的想法:

function GenerateKey($length = 16) {
    $key = '';

    for($i = 0; $i < $length; $i ++) {
        $key .= chr(mt_rand(33, 126));
    }

    return $key;
}

這有什么問題嗎?

另一個大多數其他問題都與密碼生成有關.我想要更多種類的字符,我不在乎 1l.我想要盡可能多的可能的鍵.

Another Most of the other questions deal with password generation. I want a wider variety of characters and I don't care about 1 vs l. I want the maximum number of possible keys to be possible.

注意:生成的密鑰不一定是加密安全的.

推薦答案

更新 (12/2015):對于 PHP 7.0,您應該使用 random_int() 而不是 mt_rand,因為它提供加密安全值"

Update (12/2015): For PHP 7.0, you should use random_int() instead of mt_rand as it provides "cryptographically secure values"

就個人而言,我喜歡使用 sha1(microtime(true).mt_rand(10000,90000)) 但您正在尋找更多可定制的方法,所以試試這個功能(這是一個修改您對這個答案的要求:

Personally, I like to use sha1(microtime(true).mt_rand(10000,90000)) but you are looking for more of a customizable approach, so try this function (which is a modification to your request of this answer):

function rand_char($length) {
  $random = '';
  for ($i = 0; $i < $length; $i++) {
    $random .= chr(mt_rand(33, 126));
  }
  return $random;
}

不過,這可能會比 uniqid()、md5() 或 sha1() 慢得多.

Still, this will probably be significantly slower than uniqid(), md5(), or sha1().

抱歉,您似乎是第一個知道的.:D

Looks like you got to it first, sorry. :D

編輯 2:我決定用 PHP 5 和 eAccelerator 在我的 Debian 機器上做一個不錯的小測試(原諒長代碼):

Edit 2: I decided to do a nice little test on my Debian machine with PHP 5 and eAccelerator (excuse the long code):

function rand_char($length) {
  $random = '';
  for ($i = 0; $i < $length; $i++) {
    $random .= chr(mt_rand(33, 126));
  }
  return $random;
}

function rand_sha1($length) {
  $max = ceil($length / 40);
  $random = '';
  for ($i = 0; $i < $max; $i ++) {
    $random .= sha1(microtime(true).mt_rand(10000,90000));
  }
  return substr($random, 0, $length);
}

function rand_md5($length) {
  $max = ceil($length / 32);
  $random = '';
  for ($i = 0; $i < $max; $i ++) {
    $random .= md5(microtime(true).mt_rand(10000,90000));
  }
  return substr($random, 0, $length);
}

$a = microtime(true);
for ($x = 0; $x < 1000; $x++)
  $temp = rand_char(1000);

echo "Rand:	".(microtime(true) - $a)."
";

$a = microtime(true);
for ($x = 0; $x < 1000; $x++)
  $temp = rand_sha1(1000);

echo "SHA-1:	".(microtime(true) - $a)."
";

$a = microtime(true);
for ($x = 0; $x < 1000; $x++)
  $temp = rand_md5(1000);

echo "MD5:	".(microtime(true) - $a)."
";

結果:

Rand:   2.09621596336
SHA-1:  0.611464977264
MD5:    0.618473052979

所以我的建議是,如果您想要速度(但不是完整字符集),請堅持使用 MD5、SHA-1 或 Uniqid(我還沒有測試......)

So my suggestion, if you want speed (but not full charset), is to stick to MD5, SHA-1, or Uniqid (which I didn't test.. yet)

這篇關于在 PHP 中生成隨機密鑰的最佳方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 三级免费毛片 | 欧美成人自拍 | 午夜免费观看 | 日韩一二三区 | 伊人免费在线观看 | 国产色在线 | 成年人黄色一级片 | 日韩精品中文字幕在线 | 精品视频一区二区三区在线观看 | 91麻豆产精品久久久久久 | 亚洲久视频 | 久久天堂 | 黄网站在线观看 | 国产黄色精品在线观看 | 亚洲一区视频在线 | 国产一区二区三区四区五区加勒比 | 亚洲一区二区在线播放 | 精品国产乱码久久久久久闺蜜 | 亚洲在线看 | 国产精品久久久久久久岛一牛影视 | 操夜夜 | 欧美日韩综合精品 | 亚洲网站在线观看 | 久久黄网| 国产亚洲一区二区三区在线观看 | 精品一区av | 欧美性猛交一区二区三区精品 | 国产一级一级毛片 | 久久综合九色综合欧美狠狠 | 免费视频99 | 91免费福利视频 | 久久亚洲一区二区三区四区 | 99久久精品一区二区毛片吞精 | 国产精品久久久久久久岛一牛影视 | 亚洲精品大片 | 中文字幕视频网 | 精品自拍视频 | 欧美精品一区二区三区在线 | 国产精品波多野结衣 | 二区在线视频 | 中文字幕在线精品 |