7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
24 * @see Zend_Crypt_Math_BigInteger
26 require_once 'Zend/Crypt/Math/BigInteger.php';
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Crypt_Math extends Zend_Crypt_Math_BigInteger
38 * Generate a pseudorandom number within the given range.
39 * Will attempt to read from a systems RNG if it exists or else utilises
40 * a simple random character to maximum length process. Simplicity
41 * is a factor better left for development...
43 * @param string|int $minimum
44 * @param string|int $maximum
47 public function rand($minimum, $maximum)
49 if (file_exists('/dev/urandom')) {
50 $frandom = fopen('/dev/urandom', 'r');
51 if ($frandom !== false) {
52 return fread($frandom, strlen($maximum) - 1);
55 if (strlen($maximum) < 4) {
56 return mt_rand($minimum, $maximum - 1);
59 $i2 = strlen($maximum) - 1;
60 for ($i = 1;$i < $i2;$i++) {
61 $rand .= mt_rand(0,9);
63 $rand .= mt_rand(0,9);
68 * Get the big endian two's complement of a given big integer in
74 public function btwoc($long) {
75 if (ord($long[0]) > 127) {
76 return "\x00" . $long;
82 * Translate a binary form into a big integer string
84 * @param string $binary
87 public function fromBinary($binary) {
88 return $this->_math->binaryToInteger($binary);
92 * Translate a big integer string into a binary form
94 * @param string $integer
97 public function toBinary($integer)
99 return $this->_math->integerToBinary($integer);