base32Decode(str_replace(" ", "", strtoupper($key))); $timeSlice = floor(time() / 30); $binary_timestamp = pack('N*', 0) . pack('N*', $timeSlice); $hash = hash_hmac('sha1', $binary_timestamp, $binary_key, true); $offset = ord($hash[19]) & 0xf; $otp = ( ((ord($hash[$offset + 0]) & 0x7f) << 24) | ((ord($hash[$offset + 1]) & 0xff) << 16) | ((ord($hash[$offset + 2]) & 0xff) << 8) | (ord($hash[$offset + 3]) & 0xff) ) % 1000000; return str_pad($otp, 6, '0', STR_PAD_LEFT); } private function base32Decode($base32) { $binary_key = ''; $n = 0; $j = 0; for ($i = 0; $i < strlen($base32); $i++) { $n = $n << 5; $n += strpos($this->alphabet, $base32[$i]); $j += 5; if ($j >= 8) { $j -= 8; $binary_key .= chr(($n & (0xFF << $j)) >> $j); } } return $binary_key; } public function isValidKey($secret, $CheckLength = true) { if (strlen($secret) < 16 && $CheckLength) { return false; } $pattern = '/^[A-Z2-7]+$/'; if (!preg_match($pattern, $secret)) { return false; } return true; } public function generateKey($length = 16) { $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } } ?>