Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Time.h>
- #include <Wire.h>
- #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
- #include <crypto.h>
- uint8_t * secretkey = (uint8_t *) "Hello!\xde\xad\xbe\xef";
- int secretlen = 80; //bit value
- hmac_sha1_ctx_t hctx;
- const unsigned long VERIFICATION_CODE_MODULUS = 1000000;
- uint32_t totp(uint64_t time){
- uint8_t mac[20];
- uint8_t challenge[8];
- for (int i = 8; i--; time >>= 8) {
- challenge[i] = time;
- }
- hmac_sha1_init(&hctx, secretkey, secretlen);
- hmac_sha1_lastBlock(&hctx, challenge, 64);
- hmac_sha1_final(mac, &hctx);
- uint8_t offset = mac[19] & 0xF;
- uint64_t truncated = 0;
- for(int i = 0; i < 4; ++i){
- truncated <<= 8;
- truncated |= mac[offset + i];
- }
- truncated &= 0x7FFFFFFF;
- truncated %= VERIFICATION_CODE_MODULUS;
- return (uint32_t)truncated;
- }
- void setup() {
- char totpbuf[10];
- Keyboard.begin();
- delay(1000);
- setSyncProvider(RTC.get); // the function to get the time from the RTC
- if(timeStatus()!= timeSet)
- Keyboard.println("ERROR");
- else{
- uint32_t timestamp = (now()-25200)/30;
- uint32_t totpval = totp(timestamp);
- sprintf(totpbuf,"%06lu",totpval);
- Keyboard.println(totpbuf);
- }
- }
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement