Textpattern PHP Cross Reference Content Management Systems

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

Description: Generates random bytes.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2016 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 <http://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  
  73          if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.0') >= 0) {
  74              $random = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
  75  
  76              if ($random && strlen($random) === $bytes) {
  77                  return substr(bin2hex($random), 0, $length);
  78              }
  79          }
  80  
  81          if (IS_WIN === false && file_exists('/dev/urandom') && is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== false) {
  82              if (function_exists('stream_set_read_buffer')) {
  83                  stream_set_read_buffer($fp, 0);
  84              }
  85  
  86              $random = fread($fp, $bytes);
  87              fclose($fp);
  88  
  89              if ($random && strlen($random) === $bytes) {
  90                  return substr(bin2hex($random), 0, $length);
  91              }
  92          }
  93  
  94          if (IS_WIN === false && function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4') >= 0) {
  95              $random = openssl_random_pseudo_bytes($bytes, $strong);
  96  
  97              if ($random && $strong === true && strlen($random) === $bytes) {
  98                  return substr(bin2hex($random), 0, $length);
  99              }
 100          }
 101  
 102          return parent::generate($length);
 103      }
 104  }

title

Description

title

Description

title

Description

title

title

Body