TOTP – Arduino library

by luca
TOTP – Arduino library


The Arduino TOTP (Time-based One Time Password) library implements the algorithm described in the RFC 6238 to generate time-based OTP codes.

A new code is generated every 30 seconds, from a password (better, a shared secret) and the actual timestamp value (= the number of seconds from the date 01/01/1970, called Epoch).

These OTP codes are commonly used to access web sites, home-banking applications… and are generated with tokens, that could be hardware or software ones


The library can be easily installed using the Arduino’s Library Manager dell’IDE (menu Sketch – Include Library – Manage Libraries…):


Alternatively you can download the latest release from Github and install it manually.


First, define the password and create the TOTP object passing to its constructor the pointer to the password and the password’s length:

uint8_t hmacKey[] = {0x4d, 0x79, 0x4c, 0x65, 0x67, 0x6f, 0x44, 0x6f, 0x6f, 0x72};
TOTP totp = TOTP(hmacKey, 10);

To convert a password in a byte array you can use the following webpage:

that also displays the password in base32 encoding and as a QRCode to configure the Google Authenticator app:

To get a new code, call the getCode() method passing the actual timestamp:

char* newCode = totp.getCode(timeStamp);

The timestamp value can be obtained from a RTC (real-time clock) IC, like the DS1307 from Maxim.

One of my projects that uses this library is OTP Door lock