Skip to content
Open
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
16 changes: 13 additions & 3 deletions src/TOTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/TOTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -26,7 +28,7 @@ class TOTP {
uint8_t _byteArray[8];
uint8_t* _hash;
int _offset;
long _truncatedHash;
unsigned long _truncatedHash;
char _code[7];
};

Expand Down