Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Random data for use in PHP
- *
- * Uses random_int from: https://github.com/paragonie/random_compat
- *
- * It has been test with PHPUnit. I use it.
- *
- *
- * Author RFV
- *
- * I copied all the interesting code from elsewhere - it is all MIT licensed - Please - have fun :-)
- *
- * License:
- *
- * https://opensource.org/licenses/MIT
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * You use it and you break it - it it yours.
- * You use it and it breaks - it is still yours.
- *
- * Feel free to let me know how it got broken :)
- *
- */
- namespace app\system\encryption;
- class RandomData implements /* \app\system\IForgetState */ {
- const BUFFER_LENGTH_MAX = 64;
- const NUMBER_BASE_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz"; // base 36 max
- private static $instance = null;
- private $buffer = '';
- private $curBufferPos = self::BUFFER_LENGTH_MAX;
- public function int32($min = PHP_INT_MIN, $max = PHP_INT_MAX) {
- return random_int($min, $max);
- }
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // This does: one pass: setting a fixed length string with a random character from the
- // required range of characters. For each character in turn. It is fast and certain to be as random as the source.
- public function digitStr($len, $base = 10)
- {
- #Edit this string to change the characters that can be used to create the string
- $len = min(max($len, 1), 128 * 1024);
- $baseChars = self::NUMBER_BASE_CHARS;
- $result = str_pad('', $len, $baseChars[0]);
- $maxIdx = min( max($base - 1, 1), strlen($baseChars) - 1);
- for($idx = 0; ($idx < $len); $idx++) {
- $baseIdx = $this->int32(0, $maxIdx); // Generate a random position in the $chars string
- $result[$idx] = $baseChars[$baseIdx]; // replace the character, at the current position, with the random character
- }
- return $result;
- }
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- public function hexStr($byteLength = 32)
- {
- return bin2hex(random_bytes(max($byteLength, 1)));
- }
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- public function binStr($byteLength = 32)
- {
- return random_bytes(max($byteLength, 1));
- }
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- public function Str($byteLength)
- {
- return $this->binStr($byteLength);
- }
- /**
- * A string of filtered bytes...
- *
- * @param type $byteLength
- * @param type $byteMinValue
- * @param type $byteMaxValue
- * @return type
- */
- public function filteredBytes($length = 32, $byteMinValue = 0, $byteMaxValue = 255)
- {
- $length = min(max($length, 1), 128 * 1024);
- assert($byteMinValue < $byteMaxValue && $byteMinValue >= 0 && $byteMaxValue >= 1);
- $out = str_repeat(CHR(0), $length);
- for($outIdx = 0; $length > 0; $outIdx++, $length--) {
- $out[$outIdx] = $this->filteredByte($byteMinValue, $byteMaxValue);
- }
- return $out;
- }
- /**
- * Random character in range 0 .. 255
- *
- * Is reasonably efficient as it uses a buffer
- *
- * @return character
- */
- public function byte()
- {
- if ($this->curBufferPos >= self::BUFFER_LENGTH_MAX) {
- $this->buffer = random_bytes(self::BUFFER_LENGTH_MAX);
- $this->curBufferPos = 0;
- }
- return $this->buffer[$this->curBufferPos++];
- }
- /**
- * Throw away any byte not in the requirec range...
- *
- * @param type $byteMinValue
- * @param type $byteMaxValue
- * @return type
- */
- public function filteredByte($byteMinValue = 0, $byteMaxValue = 255)
- {
- do {
- $ordValue = ORD($this->byte());
- }
- while (! ($ordValue >= $byteMinValue && $ordValue <= $byteMaxValue));
- return CHR($ordValue);
- }
- /* --------------------------------------------------------------------------
- * static stuff
- */
- public static function instance()
- {
- if (is_null(self::$instance)) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * For testing - allows resetting class variables
- */
- public static function forget()
- {
- self::$instance = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement