<bdo id='ByKCc'></bdo><ul id='ByKCc'></ul>

  • <i id='ByKCc'><tr id='ByKCc'><dt id='ByKCc'><q id='ByKCc'><span id='ByKCc'><b id='ByKCc'><form id='ByKCc'><ins id='ByKCc'></ins><ul id='ByKCc'></ul><sub id='ByKCc'></sub></form><legend id='ByKCc'></legend><bdo id='ByKCc'><pre id='ByKCc'><center id='ByKCc'></center></pre></bdo></b><th id='ByKCc'></th></span></q></dt></tr></i><div id='ByKCc'><tfoot id='ByKCc'></tfoot><dl id='ByKCc'><fieldset id='ByKCc'></fieldset></dl></div>

    <small id='ByKCc'></small><noframes id='ByKCc'>

  • <tfoot id='ByKCc'></tfoot>
      <legend id='ByKCc'><style id='ByKCc'><dir id='ByKCc'><q id='ByKCc'></q></dir></style></legend>

        用 openssl_random_pseudo_bytes() 替换 rand()

        replace rand() with openssl_random_pseudo_bytes()(用 openssl_random_pseudo_bytes() 替换 rand())
          <tbody id='ZiA4E'></tbody>
        • <bdo id='ZiA4E'></bdo><ul id='ZiA4E'></ul>
          <tfoot id='ZiA4E'></tfoot>

          • <small id='ZiA4E'></small><noframes id='ZiA4E'>

                <i id='ZiA4E'><tr id='ZiA4E'><dt id='ZiA4E'><q id='ZiA4E'><span id='ZiA4E'><b id='ZiA4E'><form id='ZiA4E'><ins id='ZiA4E'></ins><ul id='ZiA4E'></ul><sub id='ZiA4E'></sub></form><legend id='ZiA4E'></legend><bdo id='ZiA4E'><pre id='ZiA4E'><center id='ZiA4E'></center></pre></bdo></b><th id='ZiA4E'></th></span></q></dt></tr></i><div id='ZiA4E'><tfoot id='ZiA4E'></tfoot><dl id='ZiA4E'><fieldset id='ZiA4E'></fieldset></dl></div>

                  <legend id='ZiA4E'><style id='ZiA4E'><dir id='ZiA4E'><q id='ZiA4E'></q></dir></style></legend>
                  本文介绍了用 openssl_random_pseudo_bytes() 替换 rand()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要替换 PHP 的 rand() 函数,该函数使用强加密随机数生成器.

                  I need a replacement for PHP's rand() function that uses a cryptographically strong random number generator.

                  openssl_random_pseudo_bytes() 函数可让您访问强随机数生成器,但它会将其数据输出为字节字符串.相反,我需要一个介于 0 和 X 之间的整数.

                  The openssl_random_pseudo_bytes() function gets you access to the strong random number generator, but it outputs its data as a byte string. Instead, I need an integer between 0 and X.

                  我想关键是将 openssl_random_pseudo_bytes() 的输出转换为整数,然后您可以对其进行任何您需要的数学运算.我可以想到一些从字节字符串转换为整数的蛮力"方法,但我希望得到一些……优雅的东西.

                  I imagine the key is to get the output of openssl_random_pseudo_bytes() into an integer, then you can do any math on it that you need to. I can think of a few "brute force" ways of converting from a byte string to an integer, but I was hoping for something ... elegant.

                  推荐答案

                  使用提供的建议,我使用 OpenSSL 创建了一个替代 rand() 的插件.我会把它包括在这里以供后代使用.

                  Using provided suggestions, I've created a drop-in replacement for rand() using OpenSSL. I'll include it here for posterity.

                  $pedantic 选项通过在结果不会在可能的范围内均匀分布时重新开始来提供无偏差的结果.

                  The $pedantic option gives bias-free results by starting over when results won't be evenly distributed across the possible range.

                  function crypto_rand($min,$max,$pedantic=True) {
                      $diff = $max - $min;
                      if ($diff <= 0) return $min; // not so random...
                      $range = $diff + 1; // because $max is inclusive
                      $bits = ceil(log(($range),2));
                      $bytes = ceil($bits/8.0);
                      $bits_max = 1 << $bits;
                      // e.g. if $range = 3000 (bin: 101110111000)
                      //  +--------+--------+
                      //  |....1011|10111000|
                      //  +--------+--------+
                      //  bits=12, bytes=2, bits_max=2^12=4096
                      $num = 0;
                      do {
                          $num = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))) % $bits_max;
                          if ($num >= $range) {
                              if ($pedantic) continue; // start over instead of accepting bias
                              // else
                              $num = $num % $range;  // to hell with security
                          }
                          break;
                      } while (True);  // because goto attracts velociraptors
                      return $num + $min;
                  }
                  

                  这篇关于用 openssl_random_pseudo_bytes() 替换 rand()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                        <small id='GfirD'></small><noframes id='GfirD'>

                        • <legend id='GfirD'><style id='GfirD'><dir id='GfirD'><q id='GfirD'></q></dir></style></legend>
                            <bdo id='GfirD'></bdo><ul id='GfirD'></ul>

                            <i id='GfirD'><tr id='GfirD'><dt id='GfirD'><q id='GfirD'><span id='GfirD'><b id='GfirD'><form id='GfirD'><ins id='GfirD'></ins><ul id='GfirD'></ul><sub id='GfirD'></sub></form><legend id='GfirD'></legend><bdo id='GfirD'><pre id='GfirD'><center id='GfirD'></center></pre></bdo></b><th id='GfirD'></th></span></q></dt></tr></i><div id='GfirD'><tfoot id='GfirD'></tfoot><dl id='GfirD'><fieldset id='GfirD'></fieldset></dl></div>

                          • <tfoot id='GfirD'></tfoot>

                              <tbody id='GfirD'></tbody>