Yet another minimal TOTP generator.
wget https://raw.githubusercontent.com/KevCui/totp/master/totp
chmod +x ./totp
secret="${1^^}$(printf "%$(( (8-${#1}) % 8 ))s" | tr " " "=")"
key="$(base32 -d <<< "$secret" \
| xxd -p \
| tr -cd 0-9A-Fa-f)"
mac=$(printf "%016X" "$(( ($(date +%s)) / 30))" \
| xxd -r -p \
| openssl sha1 -binary -mac hmac -macopt "hexkey:$key" \
| xxd -p)
offset="$(( 16#"${mac:39:1}" * 2))"
printf "%06d\n" "$(( (0x${mac:offset:8} & 0x7FFFFFFF) % 1000000 ))"
- Generate TOTP with secret string:
~$ ./totp ZYTYYE5FOAGW5ML7LRWUL4WTZLNJAMZS
388841
- To better protect secret, combine with secret-vault to generate TOTP:
~$ ./totp "$(vault.sh -k totp_secret)"
300597
This project is heavily inspired by susam/mintotp and jakwings/bash-totp.