Created
March 12, 2025 06:09
-
-
Save zhangyoufu/48fa815c2506bc821a7f05af3d31e13b to your computer and use it in GitHub Desktop.
calculate SteamGuard OTP in shell script (standard TOTP, last step use base26 charset, truncate to 5 chars)
This file contains 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
#!/bin/bash | |
tohex() { od -An -t x1 | tr -d '[:space:]'; } | |
fromhex() { printf %b "$(echo "$1" | sed 's/../\\x&/g')"; } | |
steamguard_otp() { | |
SECRET_HEX=$(base32 -d <<<"$1" | tohex) | |
DIGEST_HEX=$(fromhex "$(printf "%016x" "$(($(date +%s)/30))")" | openssl dgst -sha1 -mac HMAC -macopt hexkey:"${SECRET_HEX}" -binary | tohex) | |
OFFSET=$((0x${DIGEST_HEX:39:1})) | |
X=$((0x${DIGEST_HEX:$((OFFSET*2)):8}&0x7FFFFFFF)) | |
CHARSET=23456789BCDFGHJKMNPQRTVWXY | |
RESULT='' | |
while [ ${#RESULT} -lt 5 ]; do | |
RESULT+=${CHARSET:$((X%26)):1} | |
X=$((X/26)) | |
done | |
echo -n "${RESULT}" | |
} | |
steamguard_otp "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment