totp

Simple cli tool for storing TOTP secrets and generating tokens
git clone https://git.inz.fi/totp/
Log | Files | Refs | Submodules

test.sh (4247B)


      1 #!/bin/sh
      2 
      3 BIN="$1"
      4 trap "test -n \"\$TMP\" && rm \"\$TMP\"" EXIT
      5 TMP="$(mktemp)"
      6 
      7 rfc6238() {
      8 	PASS="KhxvbPvY4dbyZ/zkXY+c/PCJ4lU"
      9 	rm "$TMP"
     10 	RESULT=true
     11 
     12 	# Secret = "12345678901234567890", algo SHA-1, period 30 seconds, 8 digits
     13 	"$BIN" -k "$PASS" -f "$TMP" -a "otpauth://totp/RFC6238:SHA1?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=RFC6238&algorithm=SHA1&digits=8&period=30"
     14 	# Secret = "12345678901234567890123456789012", algo SHA-256, period 30 seconds, 8 digits
     15 	"$BIN" -k "$PASS" -f "$TMP" -a "otpauth://totp/RFC6238:SHA256?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA&issuer=RFC6238&algorithm=SHA256&digits=8&period=30"
     16 	# Secret = "1234567890123456789012345678901234567890123456789012345678901234", algo SHA-256, period 30 seconds, 8 digits
     17 	"$BIN" -k "$PASS" -f "$TMP" -a "otpauth://totp/RFC6238:SHA512?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA&issuer=RFC6238&algorithm=SHA512&digits=8&period=30"
     18 
     19 	while IFS='|' read _ stamp _ _ token algo _; do
     20 		algo="$(echo $algo)"
     21 		token="$(echo $token)"
     22 		stamp="$(echo $stamp)"
     23 		gentok="$("$BIN" -k "$PASS" -f "$TMP" -T "$stamp" -t "RFC6238:$algo")"
     24 		if ! test "$token" = "$gentok"; then
     25 			echo "Token generation failed for $algo at time $stamp, got $gentok, expected $token" >&2
     26 			RESULT=false
     27 		fi
     28 		read _ || break
     29 		# Test data from RFC 6238
     30 	done <<FOO
     31 |      59     |  1970-01-01  | 0000000000000001 | 94287082 |  SHA1  |
     32 |             |   00:00:59   |                  |          |        |
     33 |      59     |  1970-01-01  | 0000000000000001 | 46119246 | SHA256 |
     34 |             |   00:00:59   |                  |          |        |
     35 |      59     |  1970-01-01  | 0000000000000001 | 90693936 | SHA512 |
     36 |             |   00:00:59   |                  |          |        |
     37 |  1111111109 |  2005-03-18  | 00000000023523EC | 07081804 |  SHA1  |
     38 |             |   01:58:29   |                  |          |        |
     39 |  1111111109 |  2005-03-18  | 00000000023523EC | 68084774 | SHA256 |
     40 |             |   01:58:29   |                  |          |        |
     41 |  1111111109 |  2005-03-18  | 00000000023523EC | 25091201 | SHA512 |
     42 |             |   01:58:29   |                  |          |        |
     43 |  1111111111 |  2005-03-18  | 00000000023523ED | 14050471 |  SHA1  |
     44 |             |   01:58:31   |                  |          |        |
     45 |  1111111111 |  2005-03-18  | 00000000023523ED | 67062674 | SHA256 |
     46 |             |   01:58:31   |                  |          |        |
     47 |  1111111111 |  2005-03-18  | 00000000023523ED | 99943326 | SHA512 |
     48 |             |   01:58:31   |                  |          |        |
     49 |  1234567890 |  2009-02-13  | 000000000273EF07 | 89005924 |  SHA1  |
     50 |             |   23:31:30   |                  |          |        |
     51 |  1234567890 |  2009-02-13  | 000000000273EF07 | 91819424 | SHA256 |
     52 |             |   23:31:30   |                  |          |        |
     53 |  1234567890 |  2009-02-13  | 000000000273EF07 | 93441116 | SHA512 |
     54 |             |   23:31:30   |                  |          |        |
     55 |  2000000000 |  2033-05-18  | 0000000003F940AA | 69279037 |  SHA1  |
     56 |             |   03:33:20   |                  |          |        |
     57 |  2000000000 |  2033-05-18  | 0000000003F940AA | 90698825 | SHA256 |
     58 |             |   03:33:20   |                  |          |        |
     59 |  2000000000 |  2033-05-18  | 0000000003F940AA | 38618901 | SHA512 |
     60 |             |   03:33:20   |                  |          |        |
     61 | 20000000000 |  2603-10-11  | 0000000027BC86AA | 65353130 |  SHA1  |
     62 |             |   11:33:20   |                  |          |        |
     63 | 20000000000 |  2603-10-11  | 0000000027BC86AA | 77737706 | SHA256 |
     64 |             |   11:33:20   |                  |          |        |
     65 | 20000000000 |  2603-10-11  | 0000000027BC86AA | 47863826 | SHA512 |
     66 |             |   11:33:20   |                  |          |        |
     67 FOO
     68 	$RESULT
     69 }
     70 
     71 existdb() {
     72 	"$BIN" -K test.pw -f dbs/test.db -l >"$TMP"
     73 	diff "$TMP" - <<FOO
     74 SHA1 by RFC6238
     75 SHA256 by RFC6238
     76 SHA512 by RFC6238
     77 FOO
     78 }
     79 
     80 legacydb() {
     81 	"$BIN" -K test.pw -f dbs/test.legacy.db -l >"$TMP"
     82 	diff "$TMP" - <<FOO
     83 SHA1 by RFC6238
     84 SHA256 by RFC6238
     85 SHA512 by RFC6238
     86 FOO
     87 }
     88 
     89 rfc6238
     90 existdb
     91 legacydb