<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()

        时间:2023-12-01
          <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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:随机睡眠可以防止定时攻击吗? 下一篇:如何在 Web 服务器中安全地从 PHP 建立 TLS 连接

                  相关文章

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

                    1. <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>

                    2. <tfoot id='GfirD'></tfoot>