15TOTP::TOTP(
const uint8_t key[],
size_t key_len, std::string_view hash_algo,
size_t digits,
size_t time_step) :
16 m_hotp(key, key_len, hash_algo, digits),
17 m_time_step(time_step),
18 m_unix_epoch(
calendar_point(1970, 1, 1, 0, 0, 0).to_std_timepoint()) {
23 BOTAN_ARG_CHECK(m_time_step > 0 && m_time_step <= 300,
"Invalid TOTP time step");
36 std::chrono::system_clock::time_point current_time,
37 size_t clock_drift_accepted,
38 size_t forward_drift_accepted) {
39 const uint64_t unix_time = std::chrono::duration_cast<std::chrono::seconds>(current_time - m_unix_epoch).count();
40 return verify_totp(otp, unix_time, clock_drift_accepted, forward_drift_accepted);
43bool TOTP::verify_totp(uint32_t otp, uint64_t unix_time,
size_t clock_drift_accepted,
size_t forward_drift_accepted) {
49 BOTAN_ARG_CHECK(clock_drift_accepted <= 10000,
"TOTP clock_drift_accepted too large");
50 BOTAN_ARG_CHECK(forward_drift_accepted <= 10000,
"TOTP forward_drift_accepted too large");
53 BOTAN_ARG_CHECK(unix_time >= 1000000000,
"TOTP unix_time argument is implausibly small");
55 const uint64_t t = unix_time / m_time_step;
57 for(
size_t i = 0; i <= clock_drift_accepted; ++i) {
59 if(m_hotp.generate_hotp(t - i) == otp) {
64 for(
size_t i = 1; i <= forward_drift_accepted; ++i) {
65 if(m_hotp.generate_hotp(t + i) == otp) {