/**
  * @covers CryptLib\Core\BaseConverter::convertToBinary
  * @covers CryptLib\Core\BaseConverter::convertFromBinary
  * @covers CryptLib\Core\BaseConverter::baseConvert
  * @dataProvider provideConvertToFromBinary
  */
 public function testConvertToAndFromBinary($str, $from)
 {
     return false;
     $result1 = BaseConverter::convertFromBinary($str, $from);
     $result = BaseConverter::convertToBinary($result1, $from);
     $this->assertEquals($str, $result);
 }
示例#2
0
 /**
  * Subtract two numbers
  *
  * @param string $left  The left argument
  * @param string $right The right argument
  *
  * @return A base-10 string of the difference of the two arguments
  */
 public function subtract($left, $right)
 {
     if (empty($left)) {
         return $right;
     } elseif (empty($right)) {
         return $left;
     } elseif ($right[0] == '-') {
         return $this->add($left, substr($right, 1));
     } elseif ($left[0] == '-') {
         return '-' . $this->add(ltrim($left, '-'), $right);
     }
     $left = $this->normalize($left);
     $right = $this->normalize($right);
     $results = $this->subtractBinary($left, $right);
     $result = BaseConverter::convertFromBinary($results[1], '0123456789');
     return $results[0] . $result;
 }
示例#3
0
 /**
  * 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;
 }