cli.c: rename key{,len} -> secret{,len} and free after use

This commit is contained in:
Lucas 2021-02-16 17:01:25 +00:00
parent dd84a23e7e
commit 87e8c2862c
1 changed files with 10 additions and 9 deletions

19
cli.c
View File

@ -42,7 +42,7 @@ int
main(int argc, char *argv[])
{
const char *errstr;
unsigned char *key;
unsigned char *secret;
char *in, *line;
struct otpcfg otpcfg;
size_t inlen, linesz;
@ -50,7 +50,7 @@ main(int argc, char *argv[])
uint64_t counter;
unsigned int period;
int32_t r;
int ch, digits, do_hotp, do_totp, keylen;
int ch, digits, do_hotp, do_totp, secretlen;
enum otp_hmac hmac;
counter = (uint64_t)time(NULL);
@ -124,19 +124,19 @@ main(int argc, char *argv[])
inlen = linelen;
}
keylen = b32_decoded_len(in, inlen);
if (keylen == -1)
secretlen = b32_decoded_len(in, inlen);
if (secretlen == -1)
errx(1, "invalid base32 string: %s", in);
key = malloc(keylen);
if (key == NULL)
secret = malloc(secretlen);
if (secret == NULL)
err(1, "malloc");
if (!b32_decode(key, keylen, in, inlen))
if (!b32_decode(secret, secretlen, in, inlen))
errx(1, "error decoding base32 string");
otpcfg.algorithm = hmac;
otpcfg.digits = digits;
otpcfg.secret = key;
otpcfg.secretlen = keylen;
otpcfg.secret = secret;
otpcfg.secretlen = secretlen;
if (do_hotp) {
otpcfg.type = OTP_HOTP;
otpcfg.u.hotp.counter = counter;
@ -147,6 +147,7 @@ main(int argc, char *argv[])
}
r = otp(&otpcfg);
free(secret);
if (r == -1)
errx(1, "couldn't calculate %cOTP", do_hotp ? 'H' : 'T');
printf("%0*" PRId32 "\n", digits, r);