/** * Encrypt the data using the supplied key, cipher * * @param string $data The data to encrypt * * @return string The encrypted data */ protected function encryptBlock($data) { $size = $this->cipher->getBlockSize(); $state = str_pad(BaseConverter::convertToBinary($this->state, '0123456789'), $size, chr(0), STR_PAD_LEFT); $stub = $this->cipher->encryptBlock($state); $this->state = $this->bigMath->add($this->state, 1); return $stub ^ $data; }
/** * Transform a string number into a binary string using base autodetection * * @param string $string The string to transform * * @return string The binary transformed number */ protected function normalize($string) { return BaseConverter::convertToBinary($string, '0123456789'); }
/** * Generate a random string of specified length. * * This uses the supplied character list for generating the new result * string. * * @param int $length The length of the generated string * @param string $characters An optional list of characters to use * * @return string The generated random string */ public function generateString($length, $characters = '') { if ($length == 0 || strlen($characters) == 1) { return ''; } elseif (empty($characters)) { // Default to base 64 $characters = '0123456789abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ./'; } //determine how many bytes to generate $bytes = ceil($length * floor(log(strlen($characters), 2) + 1.01) / 8); $rand = $this->generate($bytes); $result = BaseConverter::convertFromBinary($rand, $characters); if (strlen($result) < $length) { $result = str_pad($result, $length, $characters[0], STR_PAD_LEFT); } else { $result = substr($result, 0, $length); } return $result; }
/** * @covers CryptLib\Core\BaseConverter::baseConvert * @expectedException InvalidArgumentException */ public function testBaseConvertFailure() { BaseConverter::baseConvert(array(1), 1, 1); }