PageRenderTime 14ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/zf/library/Zend/Crypt/Math.php

http://github.com/eryx/php-framework-benchmark
PHP | 102 lines | 37 code | 7 blank | 58 comment | 5 complexity | 8341032983ae6fe29e9eae70d73053a2 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
 21 */
 22
 23/**
 24 * @see Zend_Crypt_Math_BigInteger
 25 */
 26require_once 'Zend/Crypt/Math/BigInteger.php';
 27
 28/**
 29 * @category   Zend
 30 * @package    Zend_Crypt
 31 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 33 */
 34class 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}