forked from osresearch/tpmtotp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
totp.c
55 lines (45 loc) · 980 Bytes
/
totp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Read a secret from stdin and generate the TOTP hash based on the time.
*
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include "oath.h"
int main(int argc, char *argv[])
{
int show_date = 1;
if (argc > 1 && strcmp(argv[1], "-q") == 0)
show_date = 0;
const size_t keylen = 20;
unsigned char key[keylen];
// this will fail on partial reads, unlikely
ssize_t rc = read(0, key, sizeof(key));
if (rc < 0)
{
perror("stdin");
return -1;
}
if (rc != (ssize_t) keylen)
{
fprintf(stderr, "Expected %zu bytes, read %zu\n",
keylen,
rc
);
return -1;
}
time_t now = time(NULL);
char time_str[128];
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&now));
uint32_t token = oauth_calc(now, key, keylen);
if (show_date)
printf("%s: %06d\n", time_str, token);
else
printf("%06d\n", token);
return 0;
}