Add basic CLI support for HOTP

This commit is contained in:
Lucas 2020-06-14 04:02:43 +00:00
parent a28d7cca02
commit 92539dcf58
1 changed files with 46 additions and 2 deletions

48
cli.c
View File

@ -13,8 +13,13 @@
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "err.h"
#include "otp.h"
@ -22,13 +27,52 @@
static void
usage(void)
{
fprintf(stderr, "Usage: %s\n", __progname);
fprintf(stderr, "Usage: %s [-H counter] SECRET\n", __progname);
exit(1);
}
int
main(int argc, char *argv[])
{
usage();
char *end;
uintmax_t n;
uint64_t counter;
int32_t r;
int ch, do_hotp;
counter = 0;
do_hotp = 0;
while ((ch = getopt(argc, argv, "H:")) != -1) {
switch (ch) {
case 'H':
errno = 0;
n = strtoumax(optarg, &end, 0);
if (*optarg == '\0' || *end != '\0')
errx(1, "-H: not a number");
if (errno != 0)
err(1, "-H");
else if (n > UINT64_MAX)
errx(1, "-H: out of range");
counter = n;
do_hotp = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
if (do_hotp) {
r = hotp(OTP_HMAC_SHA1, argv[0], strlen(argv[0]), counter, 6);
if (r == -1)
errx(1, "couldn't calculate HOTP");
printf("%" PRId32 "\n", r);
} else
errx(1, "TOTP unimplemented");
return 0;
}