Created
October 18, 2022 22:26
-
-
Save jsjohnst/95f34bc1f6ab46fd5c038138f0e9bfaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.