/**
* toBin
*
* Accepts a base32 string and returns an ascii binary string
*
* @param string $str The base32 string to convert
*
* @return string Ascii binary string
*
* @throws \Exception
*/
private function toBin($str)
{
if (!preg_match('/^[' . self::CSRFC3548 . ']+$/', $str)) {
throw new \Exception('Must match character set');
}
// Convert the base32 string back to a binary string
$str = implode('', array_map(array($this, '_mapbin'), str_split($str)));
// Remove the extra 0's we added
$str = preg_replace('/000(.{5})/', '$1', $str);
// Unpad if necessary
$length = \strlen($str);
$rbits = $length & 7;
if ($rbits > 0) {
$str = substr($str, 0, $length - $rbits);
}
return $str;
}