예제 #1
0
/**
 * Generates random number which is integer as has 6 digits.
 * @return Random integrer.
 */
function RandIntGen()
{
    static $ranheap = array();
    static $goseed = '';
    static $counter = 0;
    if (mt_rand(0, 5) > 2) {
        $a = mt_rand(0, 999999);
        $b = explode(" ", microtime());
        $b = str_shuffle($b[0]);
        $c = $a * 10000 + (int) ($b * 1000000);
        if ($c > 999999) {
            $c = $a * 10000 - (int) ($b * 1000000);
        }
        if ($c > 999999) {
            $c = $a * 1000 + (int) ($b * 100000);
        }
        if ($c > 999999 || $c < 99999) {
            return RandIntGen();
        }
        return abs((int) $c);
    }
    if ($counter <= 1) {
        $counter = 1024;
        $ran = strrev(RandKeyGen(1024, $goseed));
        for ($x = 0; $x < $counter; $x++) {
            array_push($ranheap, substr($ran, $x, 1));
        }
        $goseed = substr($ran, 0, 4);
    }
    $a = (int) ord(array_pop($ranheap));
    $b = explode(" ", microtime());
    $b = str_shuffle($b[0]);
    $c = $a * 10000 + (int) ($b * 1000000);
    if ($c > 999999) {
        $c = $a * 10000 - (int) ($b * 1000000);
    }
    if ($c > 999999) {
        $c = $a * 1000 + (int) ($b * 100000);
    }
    if ($c > 999999 || $c < 99999) {
        return RandIntGen();
    }
    return abs((int) $c);
}
예제 #2
0
 /**
  * Encrypts portion of a data.
  * @param Text data to be encrypted.
  * @param Cipher cipher to use (if empty, then the already set default will be used or default)
  * @param Key key for the cipher (if empty, the random key will be generated and stored into Cipher::mLastKey)
  * @param IV initialization vector to use (if empty, the random will be picked up and stored into Cipher::mLastIV)
  * @return encoded text or false - see also Cipher::last_error()
  */
 public function Encipher($text, $cipher = '', $key = '', $iv = '')
 {
     if ($cipher === '') {
         if ($this->mCipher !== '') {
             $cipher = $this->mCipher;
         } else {
             $cipher = $this->get_preferred_cipher();
         }
     }
     $cipher = $this->correct_cipher_name($cipher);
     if (!$this->VerifyCipher($cipher, true)) {
         $this->mErrorText = 'Cipher::Encipher(): ' . $this->mErrorText;
         throw new Exception($this->mErrorText);
         return false;
     }
     /* caution: key and IV sizes may be smaller! */
     $key_size = 0;
     $iv_size = 0;
     $this->validate_sizes('Encipher', $cipher, $key, $iv, $key_size, $iv_size);
     if ($this->type($cipher) === 'symmetric') {
         if ($key === '' && $this->mKey !== '') {
             $key = $this->mKey;
         } else {
             $key = RandKeyGen($key_size);
         }
         if ($iv === '' && $this->mIV !== '') {
             $iv = $this->mIV;
         } else {
             $iv = RandKeyGen($iv_size);
         }
         $this->validate_sizes('Encipher', $cipher, $key, $iv, $key_size, $iv_size);
     }
     $this->mLastKey = $key;
     $this->mLastIV = $iv;
     $this->mLastKeySize = $key_size;
     $this->mLastIVSize = $iv_size;
     $this->mLastCipher = $cipher;
     /*	$ret = $this->call_cipher_func($cipher, 'init');
     	if ($ret === false) {
     	    if ($this->mErrorText !== false && $this->mErrorText != '') {
     	        $this->mErrorText = 'Cipher: error while initializing: ' . $this->mErrorText;
     		throw new Exception($this->mErrorText);
     	    }
     	    throw new Exception("Cipher: internal error while initializing");
     	    return false;
     	}
     */
     $text = $this->call_cipher_func($cipher, 'encrypt', $cipher, $key, $text, $iv);
     if ($text === false) {
         if ($this->mErrorText !== false && $this->mErrorText != '') {
             $this->mErrorText = 'Cipher: error while encrypting: ' . $this->mErrorText;
             throw new Exception($this->mErrorText);
         }
         throw new Exception("Cipher: internal error while encrypting");
     }
     return $text;
 }