/** * @expectedException \chillerlan\Base32\Base32Exception * @expectedExceptionMessage Must match character set */ public function testToBinCharsetException() { Base32::toBin('MFRGGZDFMZTÖ'); }
/** * Calculate the code with the given secret and point in time * * @param string $secret * @param float $timeslice -> floor($timestamp / 30) * * @return string * @throws \chillerlan\GoogleAuth\AuthenticatorException */ public static function getCode($secret, $timeslice = null) { self::checkSecret($secret); // Pack time into binary string $time = str_repeat(chr(0), 4); $time .= pack('N*', self::checkTimeslice($timeslice)); // Hash it with users secret key $hmac = hash_hmac('SHA1', $time, Base32::toString($secret), true); // Use last nibble of result as index/offset $offset = ord(substr($hmac, -1)) & 0xf; // Unpack binary value, only 32 bits $value = unpack('N', substr($hmac, $offset, 4))[1] & 0x7fffffff; return str_pad($value % pow(10, self::$digits), self::$digits, '0', STR_PAD_LEFT); }