@@ -26,7 +26,7 @@ namespace hmac {
2626 if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
2727 throw std::runtime_error (" std::time failed" );
2828 }
29- std::time_t rounded = ( now / interval_sec) * interval_sec;
29+ std::time_t rounded = now - (now % interval_sec) ;
3030 return get_hmac (key, std::to_string (rounded), hash_type);
3131 }
3232
@@ -39,7 +39,7 @@ namespace hmac {
3939 if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
4040 throw std::runtime_error (" std::time failed" );
4141 }
42- std::time_t rounded = ( now / interval_sec) * interval_sec;
42+ std::time_t rounded = now - (now % interval_sec) ;
4343 if (constant_time_equals (token, get_hmac (key, std::to_string (rounded), hash_type))) return true ;
4444 if (rounded >= std::numeric_limits<std::time_t >::min () + interval_sec) {
4545 if (constant_time_equals (token, get_hmac (key, std::to_string (rounded - interval_sec), hash_type))) return true ;
@@ -59,7 +59,7 @@ namespace hmac {
5959 if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
6060 throw std::runtime_error (" std::time failed" );
6161 }
62- std::time_t rounded = ( now / interval_sec) * interval_sec;
62+ std::time_t rounded = now - (now % interval_sec) ;
6363 std::string payload = std::to_string (rounded) + " |" + fingerprint;
6464 return get_hmac (key, payload, hash_type);
6565 }
@@ -73,7 +73,7 @@ namespace hmac {
7373 if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
7474 throw std::runtime_error (" std::time failed" );
7575 }
76- std::time_t rounded = ( now / interval_sec) * interval_sec;
76+ std::time_t rounded = now - (now % interval_sec) ;
7777 std::string prefix = " |" + fingerprint;
7878 std::string payload = std::to_string (rounded) + prefix;
7979 if (constant_time_equals (token, get_hmac (key, payload, hash_type))) return true ;
@@ -164,6 +164,9 @@ namespace hmac {
164164 if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
165165 throw std::runtime_error (" std::time failed" );
166166 }
167+ if (now < 0 ) {
168+ throw std::runtime_error (" std::time returned negative value" );
169+ }
167170 uint64_t timestamp = static_cast <uint64_t >(now);
168171 return get_totp_code_at (key_ptr, key_len, timestamp, period, digits, hash_type);
169172 }
@@ -213,6 +216,9 @@ namespace hmac {
213216 if (now == static_cast <std::time_t >(-1 ) && errno != 0 ) {
214217 throw std::runtime_error (" std::time failed" );
215218 }
219+ if (now < 0 ) {
220+ throw std::runtime_error (" std::time returned negative value" );
221+ }
216222 uint64_t timestamp = static_cast <uint64_t >(now);
217223 uint64_t counter = timestamp / period;
218224 if (token == get_hotp_code (key_ptr, key_len, counter, digits, hash_type)) return true ;
0 commit comments