首先获取时间戳除以30取整,得到8个数,再补成8字节长度。
然后读取maFile文件中存储的shared_secret密钥,以该密钥为key,将上述处理后的8字节时间戳作为加密信息进行哈希运算,得到一串16进制的数。
最后从这串16进制字符串中取最后一位去掉高四做为偏移,在这串数中取四个字节,除26取余数5次得到5个数。从字母表中找到对应的值合起来就是我们要的令牌验证码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import hashlib import hmac import struct from base64 import b64decode from time import time
def get_steam_auth_code(secret: str, t: int = None) -> str: if not t: t = int(time() / 30) msg = struct.pack(">Q", t) key = b64decode(secret) mac = hmac.new(key, msg, hashlib.sha1).digest() offset = mac[-1] & 0x0f binary = struct.unpack('>L', mac[offset:offset + 4])[0] & 0x7fffffff codestr = list('23456789BCDFGHJKMNPQRTVWXY') chars = [] for _ in range(5): chars.append(codestr[binary % 26]) binary //= 26 code = ''.join(chars) return code
print(get_steam_auth_code('你的密钥', ))
|