示例#1
0
 /**
  * Decode a string using Crockford base-32
  *
  * {@see http://www.crockford.com/wrmg/base32.html}
  *
  * @param mixed   $number   The crockford number to convert
  * @param mixed   $base     Desired output base for number
  * @param boolean $checksum Crockford number includes checksum
  *
  * @return string The number decoded to desired base
  */
 public static function decode($number, $base = 10, $checksum = false)
 {
     // Clean and check number
     $crockford = strtoupper(trim(str_replace('-', '', $number)));
     if ($invalid = self::check($crockford)) {
         throw new \RuntimeException(sprintf('Found invalid characters "%s" for crockford number "%s"', implode('', $invalid), $number));
     }
     // Recode symbols (base-32) and convert to base-10
     $base32 = strtr($checksum ? substr($crockford, 0, -1) : $crockford, self::CROCKFORD_SYMBOLS, self::CROCKFORD_FLIPPED);
     $base10 = Base::convert($base32, 32, 10);
     // Verify checksum
     if ($checksum && substr($crockford, -1) != self::checksum($base10)) {
         throw new RuntimeException(sprintf('Invalid crockford checksum for {%s}: found "%s" must be "%s"', $number, substr($crockford, -1), self::checksum($base10)));
     }
     // Convert to desired base
     return Base::convert($base10, 10, $base);
 }
示例#2
0
 /**
  * Invalid characters on number must generate an exception
  */
 public function testInvalidNumberBase()
 {
     $this->setExpectedException('\\RuntimeException', 'Found invalid characters "ghi" for base-16 on number "2i4h1a3g0"');
     Base::Convert('2i4h1a3g0', 16, 36);
 }