forked from mjg59/tpmtotp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhotp.c
53 lines (43 loc) · 865 Bytes
/
hotp.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
/*
* Read a secret from stdin and generate the HOTP 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"
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: %s <counter>\n", argv[0]);
return -1;
}
const size_t keylen = 20;
unsigned char key[keylen];
uint32_t increment = atoi(argv[1]);
// 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;
}
uint32_t token = hotp_calc(increment, key, keylen);
printf("%06d\n", token);
return 0;
}