1. Trammell Hudson
  2. pebble

Source

pebble / totp.cpp

#include <stdio.h>
#include <time.h>
#include "sha1.h"

// change to your secret
static const uint8_t hmacKey1[]={
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xde, 0xad, 0xbe, 0xef
};

uint32_t auth_calc(void)
{
	const uint32_t now = time(NULL) / 30;

	uint8_t byteArray[] = {
		0,
		0,
		0,
		0,
		now >> 24,
		now >> 16,
		now >>  8,
		now >>  0,
	};

    Sha1.initHmac(hmacKey1,sizeof(hmacKey1));
    Sha1.writebytes(byteArray, 8);
    const uint8_t * const hash = Sha1.resultHmac();
  
    const unsigned offset = hash[20 - 1] & 0xF; 
    uint32_t truncatedHash = 0;
    int j;
    for (j = 0; j < 4; ++j) {
     truncatedHash <<= 8;
     truncatedHash  |= hash[offset + j];
    }
    
    truncatedHash &= 0x7FFFFFFF;
    truncatedHash %= 1000000;

	return truncatedHash;
}

int main(void)
{
	printf("%06d\n", auth_calc());
	return 0;
}