private function toBin($str)
{
if (!preg_match('/^[' . self::CSRFC3548 . ']+$/', $str)) {
throw new \InvalidArgumentException('Base64 string must match character set');
}
// Convert the base32 string back to a binary string
$str = join('', array_map(array($this, 'mapBin'), str_split($str)));
// Remove the extra 0's we added
$str = preg_replace('/000(.{5})/', '$1', $str);
// Unpad if nessicary
$length = strlen($str);
$rbits = $length & 7;
if ($rbits > 0) {
$str = substr($str, 0, $length - $rbits);
}
return $str;
}