From 1603aa242f7e866d3112abbdd7ab9f863a4d107e Mon Sep 17 00:00:00 2001 From: Ale Buzz Date: Sat, 15 Feb 2025 16:02:38 +0100 Subject: [PATCH 1/3] Added methods to return directly UNSIGNED LONG TOTP --- src/TOTP.cpp | 18 ++++++++++++++++-- src/TOTP.h | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/TOTP.cpp b/src/TOTP.cpp index 4dc902f..a311926 100644 --- a/src/TOTP.cpp +++ b/src/TOTP.cpp @@ -30,8 +30,16 @@ char* TOTP::getCode(long timeStamp) { return getCodeFromSteps(steps); } -// Generate a code, using the number of steps provided -char* TOTP::getCodeFromSteps(long steps) { +unsigned long TOTP::getIntCode(long timeStamp) { + long steps = timeStamp / _timeStep; + return getIntCodeFromSteps(steps); +} + + +uint16_t getCodeFromSteps(long steps); + +// Generate a code (integer), using the number of steps provided +uint16_t TOTP::getIntCodeFromSteps(long steps) { // STEP 0, map the number of steps in a 8-bytes array (counter value) _byteArray[0] = 0x00; @@ -60,6 +68,12 @@ char* TOTP::getCodeFromSteps(long steps) { _truncatedHash &= 0x7FFFFFFF; _truncatedHash %= 1000000; + return (unsigned long)_truncatedHash; +} + +// Generate a code, using the number of steps provided +char* TOTP::getCodeFromSteps(long steps) { + getCodeFromSteps(steps); // convert the value in string, with leading zeroes sprintf(_code, "%06ld", _truncatedHash); return _code; diff --git a/src/TOTP.h b/src/TOTP.h index a69e3df..db077ad 100644 --- a/src/TOTP.h +++ b/src/TOTP.h @@ -16,7 +16,9 @@ class TOTP { TOTP(uint8_t* hmacKey, int keyLength); TOTP(uint8_t* hmacKey, int keyLength, int timeStep); char* getCode(long timeStamp); + unsigned long getIntCode(long timeStamp); char* getCodeFromSteps(long steps); + unsigned long getIntCodeFromSteps(long steps); private: @@ -26,7 +28,7 @@ class TOTP { uint8_t _byteArray[8]; uint8_t* _hash; int _offset; - long _truncatedHash; + unsigned long _truncatedHash; char _code[7]; }; From f1bc9ff2a3ea310d98c082f60816f6046d1d7ee5 Mon Sep 17 00:00:00 2001 From: Ale Buzz Date: Sat, 15 Feb 2025 16:02:38 +0100 Subject: [PATCH 2/3] Errata Corrige --- src/TOTP.cpp | 15 +++++++++++++-- src/TOTP.h | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/TOTP.cpp b/src/TOTP.cpp index 4dc902f..364468a 100644 --- a/src/TOTP.cpp +++ b/src/TOTP.cpp @@ -30,8 +30,13 @@ char* TOTP::getCode(long timeStamp) { return getCodeFromSteps(steps); } -// Generate a code, using the number of steps provided -char* TOTP::getCodeFromSteps(long steps) { +unsigned long TOTP::getIntCode(long timeStamp) { + long steps = timeStamp / _timeStep; + return getIntCodeFromSteps(steps); +} + +// Generate a code (integer), using the number of steps provided +unsigned long TOTP::getIntCodeFromSteps(long steps) { // STEP 0, map the number of steps in a 8-bytes array (counter value) _byteArray[0] = 0x00; @@ -60,6 +65,12 @@ char* TOTP::getCodeFromSteps(long steps) { _truncatedHash &= 0x7FFFFFFF; _truncatedHash %= 1000000; + return (unsigned long)_truncatedHash; +} + +// Generate a code, using the number of steps provided +char* TOTP::getCodeFromSteps(long steps) { + getCodeFromSteps(steps); // convert the value in string, with leading zeroes sprintf(_code, "%06ld", _truncatedHash); return _code; diff --git a/src/TOTP.h b/src/TOTP.h index a69e3df..db077ad 100644 --- a/src/TOTP.h +++ b/src/TOTP.h @@ -16,7 +16,9 @@ class TOTP { TOTP(uint8_t* hmacKey, int keyLength); TOTP(uint8_t* hmacKey, int keyLength, int timeStep); char* getCode(long timeStamp); + unsigned long getIntCode(long timeStamp); char* getCodeFromSteps(long steps); + unsigned long getIntCodeFromSteps(long steps); private: @@ -26,7 +28,7 @@ class TOTP { uint8_t _byteArray[8]; uint8_t* _hash; int _offset; - long _truncatedHash; + unsigned long _truncatedHash; char _code[7]; }; From bbdb3dfcb7a03d7cc305eba0d5cf1a12ed859262 Mon Sep 17 00:00:00 2001 From: Ale Buzz Date: Sat, 15 Feb 2025 16:16:23 +0100 Subject: [PATCH 3/3] Correction --- src/TOTP.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/TOTP.cpp b/src/TOTP.cpp index dd9a38f..d766ef0 100644 --- a/src/TOTP.cpp +++ b/src/TOTP.cpp @@ -67,12 +67,6 @@ unsigned long TOTP::getIntCodeFromSteps(long steps) { return _truncatedHash; } -// Generate a code, using the number of steps provided -char* TOTP::getCodeFromSteps(long steps) { - getCodeFromSteps(steps); - return (unsigned long)_truncatedHash; -} - // Generate a code, using the number of steps provided char* TOTP::getCodeFromSteps(long steps) { getCodeFromSteps(steps);