Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Created October 18, 2022 22:26
Show Gist options
  • Save jsjohnst/95f34bc1f6ab46fd5c038138f0e9bfaf to your computer and use it in GitHub Desktop.
Save jsjohnst/95f34bc1f6ab46fd5c038138f0e9bfaf to your computer and use it in GitHub Desktop.
read -r -p 'Secret Key: ' secret_key; dgst=$(printf '%016X' $(($(date -u '+%s') / ${TOTP_PERIOD_SECONDS:=30})) | xxd -r -p | openssl dgst -sha1 -mac hmac -macopt "hexkey:$(printf $secret_key | tr '[:lower:]' '[:upper:]' | base32 -d | xxd -p)"); offset=$(( 2 * 16#${dgst: -1} )); token=$(( ( 16#${dgst:offset:8} & 0x7fffffff ) % 10**${TOTP_DIGITS:=6} )); printf "%0${TOTP_DIGITS}d\n" "$token"
@jsjohnst
Copy link
Author

jsjohnst commented Oct 19, 2022

This is meant to prove TOTP can be implemented as a Bash one-liner. To be very clear, I don’t use this and I don't recommend you do either. Requires Bash (3+) and doesn’t work as written in Zsh or potentially other shells. Tested on MacOS but should work on most other posix platforms I believe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment