diff --git a/src/TOTP.cpp b/src/TOTP.cpp index 4dc902f..d766ef0 100644 --- a/src/TOTP.cpp +++ b/src/TOTP.cpp @@ -25,13 +25,17 @@ TOTP::TOTP(uint8_t* hmacKey, int keyLength) { // Generate a code, using the timestamp provided char* TOTP::getCode(long timeStamp) { - long steps = timeStamp / _timeStep; 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 +64,12 @@ char* TOTP::getCodeFromSteps(long steps) { _truncatedHash &= 0x7FFFFFFF; _truncatedHash %= 1000000; + return _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]; };