web/lib/Zend/Crypt/Math.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     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.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Crypt
       
    17  * @subpackage Math
       
    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
       
    20  * @version    $Id: Math.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Crypt_Math_BigInteger
       
    25  */
       
    26 require_once 'Zend/Crypt/Math/BigInteger.php';
       
    27 
       
    28 /**
       
    29  * @category   Zend
       
    30  * @package    Zend_Crypt
       
    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
       
    33  */
       
    34 class Zend_Crypt_Math extends Zend_Crypt_Math_BigInteger
       
    35 {
       
    36 
       
    37     /**
       
    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...
       
    42      *
       
    43      * @param string|int $minimum
       
    44      * @param string|int $maximum
       
    45      * @return string
       
    46      */
       
    47     public function rand($minimum, $maximum)
       
    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     }
       
    66 
       
    67     /**
       
    68      * Get the big endian two's complement of a given big integer in
       
    69      * binary notation
       
    70      *
       
    71      * @param string $long
       
    72      * @return string
       
    73      */
       
    74     public function btwoc($long) {
       
    75         if (ord($long[0]) > 127) {
       
    76             return "\x00" . $long;
       
    77         }
       
    78         return $long;
       
    79     }
       
    80 
       
    81     /**
       
    82      * Translate a binary form into a big integer string
       
    83      *
       
    84      * @param string $binary
       
    85      * @return string
       
    86      */
       
    87     public function fromBinary($binary) {
       
    88         return $this->_math->binaryToInteger($binary);
       
    89     }
       
    90 
       
    91     /**
       
    92      * Translate a big integer string into a binary form
       
    93      *
       
    94      * @param string $integer
       
    95      * @return string
       
    96      */
       
    97     public function toBinary($integer)
       
    98     {
       
    99         return $this->_math->integerToBinary($integer);
       
   100     }
       
   101 
       
   102 }