Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Password/Random.php - 102 lines - 3202 bytes - Summary - Text - Print

Description: Generates random bytes.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Generates random bytes.
  26   *
  27   * Could be used as a seed or as a unique value itself.
  28   * Note that the value is not intended to be human-readable,
  29   * and as such should not be used for passwords. To generate
  30   * passwords, see \Textpattern\Password\Generator instead.
  31   *
  32   * <code>
  33   * echo Txp::get('\Textpattern\Password\Random')->generate(196);
  34   * </code>
  35   *
  36   * @since   4.6.0
  37   * @package Password
  38   * @see     \Textpattern\Password\Generator
  39   */
  40  
  41  namespace Textpattern\Password;
  42  
  43  class Random extends \Textpattern\Password\Generator
  44  {
  45      /**
  46       * {@inheritdoc}
  47       */
  48  
  49      public function getCharacterTable()
  50      {
  51          return array(
  52              'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  53              'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  54              '4', '5', '6', '7', '8', '9', '!', '@', '#', '$', '%', '&', '*', '?',
  55          );
  56      }
  57  
  58      /**
  59       * Generates random bytes as a string of given length.
  60       *
  61       * <code>
  62       * echo Txp::get('\Textpattern\Password\Random')->generate(196);
  63       * </code>
  64       *
  65       * @param  int $length The length of the generated value
  66       * @return string The value
  67       */
  68  
  69      public function generate($length)
  70      {
  71          $bytes = (int)ceil($length / 2);
  72          $random = null;
  73  
  74          if (function_exists('random_bytes') && version_compare(PHP_VERSION, '7.0') >= 0) {
  75              $random = random_bytes($bytes);
  76          }
  77  
  78          if (!$random && function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.0') >= 0) {
  79              $random = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
  80          }
  81  
  82          if (!$random && IS_WIN === false && file_exists('/dev/urandom') && is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== false) {
  83              if (function_exists('stream_set_read_buffer')) {
  84                  stream_set_read_buffer($fp, 0);
  85              }
  86  
  87              $random = fread($fp, $bytes);
  88              fclose($fp);
  89          }
  90  
  91          if (!$random && IS_WIN === false && function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4') >= 0) {
  92              $random = openssl_random_pseudo_bytes($bytes, $strong);
  93              $strong === true or $random = null;
  94          }
  95  
  96          if ($random && strlen($random) === $bytes) {
  97              return substr(bin2hex($random), 0, $length);
  98          }
  99  
 100          return parent::generate($length);
 101      }
 102  }

title

Description

title

Description

title

Description

title

title

Body