Read key from standard input if there are no arguments

This commit is contained in:
Lucas 2020-06-15 23:57:28 +00:00
parent e1eb8b8d95
commit bec943d06d
1 changed files with 23 additions and 3 deletions

26
cli.c
View File

@ -26,6 +26,8 @@
#include "mystrtonum.h"
#include "otp.h"
#define LINE_SIZE 16384
extern const char *__progname;
static void
@ -42,6 +44,8 @@ int
main(int argc, char *argv[])
{
const char *errstr;
char *key;
size_t key_len;
uint64_t counter;
unsigned int step;
int32_t r;
@ -99,16 +103,32 @@ main(int argc, char *argv[])
usage();
}
if (argc != 1)
if (argc > 1)
usage();
if (argc == 1) {
key = argv[0];
key_len = strlen(key);
} else {
key = malloc(sizeof(uint8_t) * (LINE_SIZE + 1));
if (key == NULL)
err(1, "malloc");
if (fgets(key, LINE_SIZE + 1, stdin) == NULL)
err(1, "fgets");
key_len = strlen(key);
if (key[key_len - 1] == '\n') {
key[key_len - 1] = '\0';
key_len--;
}
}
if (do_hotp) {
r = hotp(hmac, argv[0], strlen(argv[0]), counter, digits);
r = hotp(hmac, key, key_len, counter, digits);
if (r == -1)
errx(1, "couldn't calculate HOTP");
printf("%0*" PRId32 "\n", digits, r);
} else {
r = totp(hmac, argv[0], strlen(argv[0]), counter, step, digits);
r = totp(hmac, key, key_len, counter, step, digits);
if (r == -1)
errx(1, "couldn't calculate TOTP");
printf("%0*" PRId32 "\n", digits, r);