/** * Creates a new secret string given base32-encoded bytes * * @param string|util.Secret|security.SecureString $arg * @throws lang.FormatException */ public function __construct($arg) { static $table = ['2' => 0x1a, '3' => 0x1b, '4' => 0x1c, '5' => 0x1d, '6' => 0x1e, '7' => 0x1f, 'A' => 0x0, 'B' => 0x1, 'C' => 0x2, 'D' => 0x3, 'E' => 0x4, 'F' => 0x5, 'G' => 0x6, 'H' => 0x7, 'I' => 0x8, 'J' => 0x9, 'K' => 0xa, 'L' => 0xb, 'M' => 0xc, 'N' => 0xd, 'O' => 0xe, 'P' => 0xf, 'Q' => 0x10, 'R' => 0x11, 'S' => 0x12, 'T' => 0x13, 'U' => 0x14, 'V' => 0x15, 'W' => 0x16, 'X' => 0x17, 'Y' => 0x18, 'Z' => 0x19, 'a' => 0x0, 'b' => 0x1, 'c' => 0x2, 'd' => 0x3, 'e' => 0x4, 'f' => 0x5, 'g' => 0x6, 'h' => 0x7, 'i' => 0x8, 'j' => 0x9, 'k' => 0xa, 'l' => 0xb, 'm' => 0xc, 'n' => 0xd, 'o' => 0xe, 'p' => 0xf, 'q' => 0x10, 'r' => 0x11, 's' => 0x12, 't' => 0x13, 'u' => 0x14, 'v' => 0x15, 'w' => 0x16, 'x' => 0x17, 'y' => 0x18, 'z' => 0x19, '0' => 0xe, '1' => 0xb, '8' => 0x1]; if ($arg instanceof \security\SecureString) { $encoded = $arg->getCharacters(); } else { if ($arg instanceof \util\Secret) { $encoded = $arg->reveal(); } else { $encoded = (string) $arg; } } $buffer = 0; $left = 0; $bytes = ''; for ($i = 0, $l = strlen($encoded); $i < $l; $i++) { $c = $encoded[$i]; if (isset($table[$c])) { $buffer = $buffer << 5 | $table[$c]; $left += 5; if ($left >= 8) { $bytes .= chr($buffer >> ($left -= 8)); } } else { throw new FormatException(sprintf('Illegal character 0x%02x in input at position %d/%d', ord($c), $i, $l)); } } parent::__construct($bytes); }
/** * Creates a new secret string given base32-encoded bytes * * @param util.Bytes|string $bytes * @throws lang.FormatException */ public function __construct($bytes) { if ($bytes instanceof \lang\types\Bytes) { parent::__construct($bytes->buffer); } else { parent::__construct((string) $bytes); } }