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