/**
  * @covers PasswordLib\Core\BaseConverter::baseConvert
  * @expectedException InvalidArgumentException
  */
 public function testBaseConvertFailure()
 {
     BaseConverter::baseConvert(array(1), 1, 1);
 }
Example #2
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) / 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;
 }
Example #3
0
 /**
  * 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');
 }