diff --git a/cli.c b/cli.c
index 4d945e8..7c0c50a 100644
--- a/cli.c
+++ b/cli.c
@@ -13,8 +13,13 @@
* .
*/
+#include
+#include
+#include
#include
#include
+#include
+#include
#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;
}