Skip to content

Instantly share code, notes, and snippets.

@zhangyoufu
Created March 12, 2025 06:09
Show Gist options
  • Save zhangyoufu/48fa815c2506bc821a7f05af3d31e13b to your computer and use it in GitHub Desktop.
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)
#!/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