Read key from standard input if there are no arguments
This commit is contained in:
parent
e1eb8b8d95
commit
bec943d06d
26
cli.c
26
cli.c
@ -26,6 +26,8 @@
|
|||||||
#include "mystrtonum.h"
|
#include "mystrtonum.h"
|
||||||
#include "otp.h"
|
#include "otp.h"
|
||||||
|
|
||||||
|
#define LINE_SIZE 16384
|
||||||
|
|
||||||
extern const char *__progname;
|
extern const char *__progname;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -42,6 +44,8 @@ int
|
|||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
const char *errstr;
|
const char *errstr;
|
||||||
|
char *key;
|
||||||
|
size_t key_len;
|
||||||
uint64_t counter;
|
uint64_t counter;
|
||||||
unsigned int step;
|
unsigned int step;
|
||||||
int32_t r;
|
int32_t r;
|
||||||
@ -99,16 +103,32 @@ main(int argc, char *argv[])
|
|||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc != 1)
|
if (argc > 1)
|
||||||
usage();
|
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) {
|
if (do_hotp) {
|
||||||
r = hotp(hmac, argv[0], strlen(argv[0]), counter, digits);
|
r = hotp(hmac, key, key_len, counter, digits);
|
||||||
if (r == -1)
|
if (r == -1)
|
||||||
errx(1, "couldn't calculate HOTP");
|
errx(1, "couldn't calculate HOTP");
|
||||||
printf("%0*" PRId32 "\n", digits, r);
|
printf("%0*" PRId32 "\n", digits, r);
|
||||||
} else {
|
} else {
|
||||||
r = totp(hmac, argv[0], strlen(argv[0]), counter, step, digits);
|
r = totp(hmac, key, key_len, counter, step, digits);
|
||||||
if (r == -1)
|
if (r == -1)
|
||||||
errx(1, "couldn't calculate TOTP");
|
errx(1, "couldn't calculate TOTP");
|
||||||
printf("%0*" PRId32 "\n", digits, r);
|
printf("%0*" PRId32 "\n", digits, r);
|
||||||
|
Loading…
Reference in New Issue
Block a user