Advertisement
rfv123

PHP -class RandomData - in case someone wants to look at it

Nov 6th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.03 KB | None | 0 0
  1. <?php
  2. /*
  3.  *  Random data for use in PHP
  4.  *
  5.  * Uses random_int from: https://github.com/paragonie/random_compat
  6.  *
  7.  * It has been test with PHPUnit. I use it.
  8.  *
  9.  *
  10.  * Author RFV
  11.  *
  12.  * I copied all the interesting code from elsewhere - it is all MIT licensed - Please - have fun :-)
  13.  *
  14.  * License:
  15.  *  
  16.  * https://opensource.org/licenses/MIT
  17.  *
  18.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  19.  * of this software and associated documentation files (the "Software"), to deal
  20.  * in the Software without restriction, including without limitation the rights
  21.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22.  * copies of the Software, and to permit persons to whom the Software is
  23.  * furnished to do so, subject to the following conditions:
  24.  *
  25.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  28.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  31.  * THE SOFTWARE.
  32.  *
  33.  * You use it and you break it - it it yours.
  34.  * You use it and it breaks - it is still yours.
  35.  *
  36.  * Feel free to let me know how it got broken :)
  37.  *
  38.  */
  39.  
  40. namespace app\system\encryption;
  41.  
  42.  
  43. class RandomData implements /* \app\system\IForgetState */ {
  44.  
  45.     const BUFFER_LENGTH_MAX = 64;
  46.  
  47.     const NUMBER_BASE_CHARS =  "0123456789abcdefghijklmnopqrstuvwxyz"; // base 36 max
  48.  
  49.     private static $instance = null;
  50.  
  51.     private $buffer = '';
  52.     private $curBufferPos = self::BUFFER_LENGTH_MAX;
  53.  
  54.  
  55. public function int32($min = PHP_INT_MIN, $max = PHP_INT_MAX) {
  56.     return random_int($min, $max);
  57. }
  58.  
  59. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  60. // This does: one pass: setting a fixed length string with a random character from the
  61. // required range of characters. For each character in turn. It is fast and certain to be as random as the source.
  62.    
  63. public function digitStr($len, $base = 10)
  64. {
  65.     #Edit this string to change the characters that can be used to create the string
  66.    $len = min(max($len, 1), 128 * 1024);
  67.     $baseChars = self::NUMBER_BASE_CHARS;
  68.  
  69.     $result = str_pad('', $len, $baseChars[0]);
  70.     $maxIdx = min( max($base - 1, 1), strlen($baseChars) - 1);
  71.  
  72.  
  73.     for($idx = 0; ($idx < $len); $idx++) {
  74.         $baseIdx = $this->int32(0, $maxIdx);   // Generate a random position in the $chars string
  75.         $result[$idx] = $baseChars[$baseIdx];  // replace the character, at the current position, with the random character
  76.     }
  77.  
  78.     return $result;
  79. }
  80.  
  81. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  82. public function hexStr($byteLength = 32)
  83. {
  84.     return bin2hex(random_bytes(max($byteLength, 1)));
  85. }
  86.  
  87. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  88. public function binStr($byteLength = 32)
  89. {
  90.     return random_bytes(max($byteLength, 1));
  91. }
  92.  
  93. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  94. public function Str($byteLength)
  95. {
  96.     return $this->binStr($byteLength);
  97. }
  98.  
  99. /**
  100.  * A string of filtered bytes...
  101.  *
  102.  * @param type $byteLength
  103.  * @param type $byteMinValue
  104.  * @param type $byteMaxValue
  105.  * @return type
  106.  */
  107. public function filteredBytes($length = 32, $byteMinValue = 0, $byteMaxValue = 255)
  108. {
  109.     $length = min(max($length, 1), 128 * 1024);
  110.     assert($byteMinValue < $byteMaxValue && $byteMinValue >= 0 && $byteMaxValue >= 1);
  111.  
  112.     $out = str_repeat(CHR(0), $length);
  113.     for($outIdx = 0; $length > 0; $outIdx++, $length--) {
  114.         $out[$outIdx] = $this->filteredByte($byteMinValue, $byteMaxValue);
  115.     }
  116.     return $out;
  117. }
  118.  
  119. /**
  120.  * Random character in range 0 .. 255
  121.  *
  122.  * Is reasonably efficient as it uses a buffer
  123.  *
  124.  * @return character
  125.  */
  126. public function byte()
  127. {
  128.     if ($this->curBufferPos >= self::BUFFER_LENGTH_MAX) {
  129.         $this->buffer = random_bytes(self::BUFFER_LENGTH_MAX);
  130.         $this->curBufferPos = 0;
  131.     }
  132.     return $this->buffer[$this->curBufferPos++];
  133. }
  134.  
  135. /**
  136.  * Throw away any byte not in the requirec range...
  137.  *
  138.  * @param type $byteMinValue
  139.  * @param type $byteMaxValue
  140.  * @return type
  141.  */
  142. public function filteredByte($byteMinValue = 0, $byteMaxValue = 255)
  143. {
  144.     do {
  145.         $ordValue = ORD($this->byte());
  146.     }
  147.     while (! ($ordValue >= $byteMinValue && $ordValue <= $byteMaxValue));
  148.     return CHR($ordValue);
  149. }
  150.  
  151.  
  152. /* --------------------------------------------------------------------------
  153.  *  static stuff
  154.  */
  155.  
  156. public static function instance()
  157. {
  158.     if (is_null(self::$instance)) {
  159.         self::$instance = new static();
  160.     }
  161.  
  162.     return self::$instance;
  163. }
  164.  
  165. /**
  166.  * For testing - allows resetting class variables
  167.  */
  168. public static function forget()
  169. {
  170.     self::$instance = null;
  171. }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement