Add options for configuring digits and time step
Default digits to 6 and time step to 30 seconds. This implies passing `-d 8' to all TOTP test cases.
This commit is contained in:
		
							parent
							
								
									95341d4ca2
								
							
						
					
					
						commit
						ecc23ff01f
					
				
					 2 changed files with 40 additions and 25 deletions
				
			
		
							
								
								
									
										29
									
								
								cli.c
									
										
									
									
									
								
							
							
						
						
									
										29
									
								
								cli.c
									
										
									
									
									
								
							|  | @ -31,7 +31,9 @@ static void | |||
| usage(void) | ||||
| { | ||||
| 	fprintf(stderr, "Usage:\n" | ||||
| 	    "    %s [-h HMAC] [-H counter | -T counter] SECRET\n", __progname); | ||||
| 	    "    %s [-d digits] [-h HMAC] -H counter SECRET\n" | ||||
| 	    "    %s [-d digits] [-h HMAC] [-s step] [-T counter] SECRET\n", | ||||
| 	    __progname, __progname); | ||||
| 	exit(1); | ||||
| } | ||||
| 
 | ||||
|  | @ -40,15 +42,23 @@ main(int argc, char *argv[]) | |||
| { | ||||
| 	const char *errstr; | ||||
| 	uint64_t counter; | ||||
| 	unsigned int step; | ||||
| 	int32_t r; | ||||
| 	int ch, do_hotp; | ||||
| 	int ch, digits, do_hotp; | ||||
| 	enum otp_hmac hmac; | ||||
| 
 | ||||
| 	counter = 0; | ||||
| 	digits = 6; | ||||
| 	do_hotp = 0; | ||||
| 	hmac = OTP_HMAC_SHA1; | ||||
| 	while ((ch = getopt(argc, argv, "H:h:T:")) != -1) { | ||||
| 	step = 30; | ||||
| 	while ((ch = getopt(argc, argv, "d:H:h:s:T:")) != -1) { | ||||
| 		switch (ch) { | ||||
| 		case 'd': | ||||
| 			digits = mystrtonum(optarg, 6, 10, &errstr); | ||||
| 			if (errstr != NULL) | ||||
| 				errx(1, "digits is %s: %s", errstr, optarg); | ||||
| 			break; | ||||
| 		case 'H': | ||||
| 			counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr); | ||||
| 			if (errstr != NULL) | ||||
|  | @ -65,6 +75,11 @@ main(int argc, char *argv[]) | |||
| 			else | ||||
| 				usage(); | ||||
| 			break; | ||||
| 		case 's': | ||||
| 			step = mystrtonum(optarg, 1, UINT_MAX, &errstr); | ||||
| 			if (errstr != NULL) | ||||
| 				errx(1, "step is %s: %s", errstr, optarg); | ||||
| 			break; | ||||
| 		case 'T': | ||||
| 			counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr); | ||||
| 			if (errstr != NULL) | ||||
|  | @ -82,15 +97,15 @@ main(int argc, char *argv[]) | |||
| 		usage(); | ||||
| 
 | ||||
| 	if (do_hotp) { | ||||
| 		r = hotp(hmac, argv[0], strlen(argv[0]), counter, 6); | ||||
| 		r = hotp(hmac, argv[0], strlen(argv[0]), counter, digits); | ||||
| 		if (r == -1) | ||||
| 			errx(1, "couldn't calculate HOTP"); | ||||
| 		printf("%0*" PRId32 "\n", 6, r); | ||||
| 		printf("%0*" PRId32 "\n", digits, r); | ||||
| 	} else { | ||||
| 		r = totp(hmac, argv[0], strlen(argv[0]), counter, 30, 8); | ||||
| 		r = totp(hmac, argv[0], strlen(argv[0]), counter, step, digits); | ||||
| 		if (r == -1) | ||||
| 			errx(1, "couldn't calculate TOTP"); | ||||
| 		printf("%0*" PRId32 "\n", 8, r); | ||||
| 		printf("%0*" PRId32 "\n", digits, r); | ||||
| 	} | ||||
| 
 | ||||
| 	return 0; | ||||
|  |  | |||
							
								
								
									
										36
									
								
								run-tests.sh
									
										
									
									
									
								
							
							
						
						
									
										36
									
								
								run-tests.sh
									
										
									
									
									
								
							|  | @ -48,24 +48,24 @@ case_eq 162583 ./otpcli -H 7 "$HOTP_SECRET" | |||
| case_eq 399871 ./otpcli -H 8 "$HOTP_SECRET" | ||||
| case_eq 520489 ./otpcli -H 9 "$HOTP_SECRET" | ||||
| 
 | ||||
| case_eq 94287082 ./otpcli -T 59 "$TOTP_SECRET" | ||||
| case_eq 46119246 ./otpcli -h sha256 -T 59 "$TOTP_SHA256_SECRET" | ||||
| case_eq 90693936 ./otpcli -h sha512 -T 59 "$TOTP_SHA512_SECRET" | ||||
| case_eq 07081804 ./otpcli -T 1111111109 "$TOTP_SECRET" | ||||
| case_eq 68084774 ./otpcli -h sha256 -T 1111111109 "$TOTP_SHA256_SECRET" | ||||
| case_eq 25091201 ./otpcli -h sha512 -T 1111111109 "$TOTP_SHA512_SECRET" | ||||
| case_eq 14050471 ./otpcli -T 1111111111 "$TOTP_SECRET" | ||||
| case_eq 67062674 ./otpcli -h sha256 -T 1111111111 "$TOTP_SHA256_SECRET" | ||||
| case_eq 99943326 ./otpcli -h sha512 -T 1111111111 "$TOTP_SHA512_SECRET" | ||||
| case_eq 89005924 ./otpcli -T 1234567890 "$TOTP_SECRET" | ||||
| case_eq 91819424 ./otpcli -h sha256 -T 1234567890 "$TOTP_SHA256_SECRET" | ||||
| case_eq 93441116 ./otpcli -h sha512 -T 1234567890 "$TOTP_SHA512_SECRET" | ||||
| case_eq 69279037 ./otpcli -T 2000000000 "$TOTP_SECRET" | ||||
| case_eq 90698825 ./otpcli -h sha256 -T 2000000000 "$TOTP_SHA256_SECRET" | ||||
| case_eq 38618901 ./otpcli -h sha512 -T 2000000000 "$TOTP_SHA512_SECRET" | ||||
| case_eq 65353130 ./otpcli -T 20000000000 "$TOTP_SECRET" | ||||
| case_eq 77737706 ./otpcli -h sha256 -T 20000000000 "$TOTP_SHA256_SECRET" | ||||
| case_eq 47863826 ./otpcli -h sha512 -T 20000000000 "$TOTP_SHA512_SECRET" | ||||
| case_eq 94287082 ./otpcli -d 8 -T 59 "$TOTP_SECRET" | ||||
| case_eq 46119246 ./otpcli -d 8 -h sha256 -T 59 "$TOTP_SHA256_SECRET" | ||||
| case_eq 90693936 ./otpcli -d 8 -h sha512 -T 59 "$TOTP_SHA512_SECRET" | ||||
| case_eq 07081804 ./otpcli -d 8 -T 1111111109 "$TOTP_SECRET" | ||||
| case_eq 68084774 ./otpcli -d 8 -h sha256 -T 1111111109 "$TOTP_SHA256_SECRET" | ||||
| case_eq 25091201 ./otpcli -d 8 -h sha512 -T 1111111109 "$TOTP_SHA512_SECRET" | ||||
| case_eq 14050471 ./otpcli -d 8 -T 1111111111 "$TOTP_SECRET" | ||||
| case_eq 67062674 ./otpcli -d 8 -h sha256 -T 1111111111 "$TOTP_SHA256_SECRET" | ||||
| case_eq 99943326 ./otpcli -d 8 -h sha512 -T 1111111111 "$TOTP_SHA512_SECRET" | ||||
| case_eq 89005924 ./otpcli -d 8 -T 1234567890 "$TOTP_SECRET" | ||||
| case_eq 91819424 ./otpcli -d 8 -h sha256 -T 1234567890 "$TOTP_SHA256_SECRET" | ||||
| case_eq 93441116 ./otpcli -d 8 -h sha512 -T 1234567890 "$TOTP_SHA512_SECRET" | ||||
| case_eq 69279037 ./otpcli -d 8 -T 2000000000 "$TOTP_SECRET" | ||||
| case_eq 90698825 ./otpcli -d 8 -h sha256 -T 2000000000 "$TOTP_SHA256_SECRET" | ||||
| case_eq 38618901 ./otpcli -d 8 -h sha512 -T 2000000000 "$TOTP_SHA512_SECRET" | ||||
| case_eq 65353130 ./otpcli -d 8 -T 20000000000 "$TOTP_SECRET" | ||||
| case_eq 77737706 ./otpcli -d 8 -h sha256 -T 20000000000 "$TOTP_SHA256_SECRET" | ||||
| case_eq 47863826 ./otpcli -d 8 -h sha512 -T 20000000000 "$TOTP_SHA512_SECRET" | ||||
| 
 | ||||
| if [ $_test_rc -eq 0 ]; then | ||||
| 	printf "All %u tests completed successfully!\n" "$_test_nr" >&2 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue