Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions
Zend_Crypt_Math Class Reference
Inheritance diagram for Zend_Crypt_Math:
Zend_Crypt_Math_BigInteger

Public Member Functions

 rand ($minimum, $maximum)
 
 btwoc ($long)
 
 fromBinary ($binary)
 
 toBinary ($integer)
 
- Public Member Functions inherited from Zend_Crypt_Math_BigInteger
 __construct ($extension=null)
 
 __call ($methodName, $args)
 

Static Public Member Functions

static randBytes ($length, $strong=false)
 
static randInteger ($min, $max, $strong=false)
 

Additional Inherited Members

- Protected Member Functions inherited from Zend_Crypt_Math_BigInteger
 _loadAdapter ($extension=null)
 
- Protected Attributes inherited from Zend_Crypt_Math_BigInteger
 $_math = null
 

Detailed Description

Definition at line 34 of file Math.php.

Member Function Documentation

◆ btwoc()

btwoc (   $long)

Get the big endian two's complement of a given big integer in binary notation

Parameters
string$long
Returns
string

Definition at line 161 of file Math.php.

162  {
163  if (ord($long[0]) > 127) {
164  return "\x00" . $long;
165  }
166  return $long;
167  }

◆ fromBinary()

fromBinary (   $binary)

Translate a binary form into a big integer string

Parameters
string$binary
Returns
string

Definition at line 175 of file Math.php.

176  {
177  return $this->_math->binaryToInteger($binary);
178  }

◆ rand()

rand (   $minimum,
  $maximum 
)

Generate a pseudorandom number within the given range. Will attempt to read from a systems RNG if it exists or else utilises a simple random character to maximum length process. Simplicity is a factor better left for development...

Parameters
string | int$minimum
string | int$maximum
Returns
string

Definition at line 47 of file Math.php.

48  {
49  if (file_exists('/dev/urandom')) {
50  $frandom = fopen('/dev/urandom', 'r');
51  if ($frandom !== false) {
52  return fread($frandom, strlen($maximum) - 1);
53  }
54  }
55  if (strlen($maximum) < 4) {
56  return mt_rand($minimum, $maximum - 1);
57  }
58  $rand = '';
59  $i2 = strlen($maximum) - 1;
60  for ($i = 1; $i < $i2; $i++) {
61  $rand .= mt_rand(0, 9);
62  }
63  $rand .= mt_rand(0, 9);
64  return $rand;
65  }
$i
Definition: gallery.phtml:31

◆ randBytes()

static randBytes (   $length,
  $strong = false 
)
static

Return a random strings of $length bytes

Parameters
integer$length
boolean$strong
Returns
string

Definition at line 74 of file Math.php.

75  {
76  $length = (int) $length;
77  if ($length <= 0) {
78  return false;
79  }
80  if (function_exists('random_bytes')) { // available in PHP 7
81  return random_bytes($length);
82  }
83  if (function_exists('mcrypt_create_iv')) {
84  $bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
85  if ($bytes !== false && strlen($bytes) === $length) {
86  return $bytes;
87  }
88  }
89  if (file_exists('/dev/urandom') && is_readable('/dev/urandom')) {
90  $frandom = fopen('/dev/urandom', 'r');
91  if ($frandom !== false) {
92  return fread($frandom, $length);
93  }
94  }
95  if (true === $strong) {
96  require_once 'Zend/Crypt/Exception.php';
97  throw new Zend_Crypt_Exception(
98  'This PHP environment doesn\'t support secure random number generation. ' .
99  'Please consider installing the OpenSSL and/or Mcrypt extensions'
100  );
101  }
102  $rand = '';
103  for ($i = 0; $i < $length; $i++) {
104  $rand .= chr(mt_rand(0, 255));
105  }
106  return $rand;
107  }
$i
Definition: gallery.phtml:31

◆ randInteger()

static randInteger (   $min,
  $max,
  $strong = false 
)
static

Return a random integer between $min and $max

Parameters
integer$min
integer$max
boolean$strong
Returns
integer

Definition at line 117 of file Math.php.

118  {
119  if ($min > $max) {
120  require_once 'Zend/Crypt/Exception.php';
121  throw new Zend_Crypt_Exception(
122  'The min parameter must be lower than max parameter'
123  );
124  }
125  $range = $max - $min;
126  if ($range == 0) {
127  return $max;
128  } elseif ($range > PHP_INT_MAX || is_float($range)) {
129  require_once 'Zend/Crypt/Exception.php';
130  throw new Zend_Crypt_Exception(
131  'The supplied range is too great to generate'
132  );
133  }
134  if (function_exists('random_int')) { // available in PHP 7
135  return random_int($min, $max);
136  }
137  // calculate number of bits required to store range on this machine
138  $r = $range;
139  $bits = 0;
140  while ($r) {
141  $bits++;
142  $r >>= 1;
143  }
144  $bits = (int) max($bits, 1);
145  $bytes = (int) max(ceil($bits / 8), 1);
146  $filter = (int) ((1 << $bits) - 1);
147  do {
148  $rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
149  $rnd &= $filter;
150  } while ($rnd > $range);
151  return ($min + $rnd);
152  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ toBinary()

toBinary (   $integer)

Translate a big integer string into a binary form

Parameters
string$integer
Returns
string

Definition at line 186 of file Math.php.

187  {
188  return $this->_math->integerToBinary($integer);
189  }

The documentation for this class was generated from the following file: