Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions hmac_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ namespace hmac {
const void* key_ptr,
size_t key_len,
uint64_t timestamp,
int period,
int digits,
int period,
int digits,
TypeHash hash_type) {
if (period <= 0 || digits <= 0 || digits > 9) return 0;
// Validate period and digit parameters
if (period <= 0) {
throw std::invalid_argument("TOTP: period must be positive");
}
if (digits < 1 || digits > 9) {
throw std::invalid_argument("TOTP: digits must be in range [1, 9]");
}
uint64_t counter = timestamp / period;
return get_hotp_code(key_ptr, key_len, counter, digits, hash_type);
}
Expand All @@ -99,6 +105,13 @@ namespace hmac {
int period,
int digits,
TypeHash hash_type) {
// Validate period and digit parameters
if (period <= 0) {
throw std::invalid_argument("TOTP: period must be positive");
}
if (digits < 1 || digits > 9) {
throw std::invalid_argument("TOTP: digits must be in range [1, 9]");
}
uint64_t timestamp = static_cast<uint64_t>(std::time(nullptr));
return get_totp_code_at(key_ptr, key_len, timestamp, period, digits, hash_type);
}
Expand All @@ -111,6 +124,13 @@ namespace hmac {
int period,
int digits,
TypeHash hash_type) {
// Validate period and digit parameters
if (period <= 0) {
throw std::invalid_argument("TOTP: period must be positive");
}
if (digits < 1 || digits > 9) {
throw std::invalid_argument("TOTP: digits must be in range [1, 9]");
}
uint64_t counter = timestamp / period;
if (token == get_hotp_code(key_ptr, key_len, counter, digits, hash_type)) return true;
if (token == get_hotp_code(key_ptr, key_len, counter + 1, digits, hash_type)) return true;
Expand All @@ -127,6 +147,13 @@ namespace hmac {
int period,
int digits,
TypeHash hash_type) {
// Validate period and digit parameters
if (period <= 0) {
throw std::invalid_argument("TOTP: period must be positive");
}
if (digits < 1 || digits > 9) {
throw std::invalid_argument("TOTP: digits must be in range [1, 9]");
}
uint64_t timestamp = static_cast<uint64_t>(std::time(nullptr));
uint64_t counter = timestamp / period;
if (token == get_hotp_code(key_ptr, key_len, counter, digits, hash_type)) return true;
Expand Down
38 changes: 25 additions & 13 deletions hmac_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ namespace hmac {
/// \param digits Number of digits in the resulting OTP code (1 to 9, default: 6)
/// \param hash_type Hash function to use (SHA1, SHA256, SHA512; default: SHA1)
/// \return TOTP code as an integer
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
int get_totp_code_at(
const void* key_ptr,
size_t key_len,
uint64_t timestamp,
int period = 30,
int digits = 6,
const void* key_ptr,
size_t key_len,
uint64_t timestamp,
int period = 30,
int digits = 6,
TypeHash hash_type = TypeHash::SHA1);

/// \brief Computes TOTP code for a specific timestamp from a vector-based key
Expand All @@ -97,11 +98,12 @@ namespace hmac {
/// \param digits Number of digits in the resulting OTP code (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return TOTP code as an integer
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
template<typename T>
inline int get_totp_code_at(
const std::vector<T>& key,
const std::vector<T>& key,
uint64_t timestamp,
int period = 30,
int period = 30,
int digits = 6,
TypeHash hash_type = TypeHash::SHA1) {
static_assert(std::is_same<T, char>::value || std::is_same<T, uint8_t>::value,
Expand All @@ -116,11 +118,12 @@ namespace hmac {
/// \param digits Number of digits in the resulting OTP code (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return TOTP code as an integer
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
inline int get_totp_code_at(
const std::string& key,
const std::string& key,
uint64_t timestamp,
int period = 30,
int digits = 6,
int period = 30,
int digits = 6,
TypeHash hash_type = TypeHash::SHA1) {
return get_totp_code_at(key.data(), key.size(), timestamp, period, digits, hash_type);
}
Expand All @@ -132,11 +135,12 @@ namespace hmac {
/// \param digits Number of digits in the resulting OTP code (default: 6).
/// \param hash_type Hash function to use (default: SHA1).
/// \return TOTP code as an integer.
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9].
int get_totp_code(
const void* key_ptr,
const void* key_ptr,
size_t key_len,
int period = 30,
int digits = 6,
int period = 30,
int digits = 6,
TypeHash hash_type = TypeHash::SHA1);

/// \brief Computes current TOTP code from a vector-based key using system time (UTC)
Expand All @@ -146,6 +150,7 @@ namespace hmac {
/// \param digits Number of digits in the resulting OTP code (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return TOTP code as an integer
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
template<typename T>
inline int get_totp_code(const std::vector<T>& key, int period = 30, int digits = 6, TypeHash hash_type = TypeHash::SHA1) {
return get_totp_code(key.data(), key.size(), period, digits, hash_type);
Expand All @@ -157,6 +162,7 @@ namespace hmac {
/// \param digits Number of digits in the resulting OTP code (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return TOTP code as an integer
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
inline int get_totp_code(const std::string& key, int period = 30, int digits = 6, TypeHash hash_type = TypeHash::SHA1) {
return get_totp_code(key.data(), key.size(), period, digits, hash_type);
}
Expand All @@ -170,6 +176,7 @@ namespace hmac {
/// \param digits Expected number of digits in the OTP (default: 6)
/// \param hash_type Hash algorithm to use (SHA1, SHA256, SHA512). Default is SHA1
/// \return true if the token is valid within [-1, 0, +1] time step range; false otherwise
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
bool is_totp_token_valid(
int token,
const void* key_ptr,
Expand All @@ -188,6 +195,7 @@ namespace hmac {
/// \param digits Number of digits in the OTP (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return true if the token is valid within [-1, 0, +1] time step range; false otherwise
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
template<typename T>
inline bool is_totp_token_valid(
int token,
Expand All @@ -209,6 +217,7 @@ namespace hmac {
/// \param digits Number of digits in the OTP (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return true if the token is valid within [-1, 0, +1] time step range; false otherwise
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
inline bool is_totp_token_valid(
int token,
const std::string& key,
Expand All @@ -227,6 +236,7 @@ namespace hmac {
/// \param digits Expected number of digits in the OTP (default: 6)
/// \param hash_type Hash algorithm to use (default: SHA1)
/// \return true if the token is valid within [-1, 0, +1] time step range; false otherwise
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
bool is_totp_token_valid(
int token,
const void* key_ptr,
Expand All @@ -243,6 +253,7 @@ namespace hmac {
/// \param digits Number of digits in the OTP (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return true if the token is valid within [-1, 0, +1] time step range; false otherwise
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
template<typename T>
inline bool is_totp_token_valid(
int token,
Expand All @@ -262,6 +273,7 @@ namespace hmac {
/// \param digits Number of digits in the OTP (default: 6)
/// \param hash_type Hash function to use (default: SHA1)
/// \return true if the token is valid within [-1, 0, +1] time step range; false otherwise
/// \throws std::invalid_argument if period <= 0 or digits not in [1,9]
inline bool is_totp_token_valid(
int token,
const std::string& key,
Expand Down
Loading