生成验证码
# -*- encoding: utf-8 -*-"
# @Module : google auth
import hmac, base64, struct, hashlib, time
def get_hotp_token(secret, intervals_no):
key = base64.b32decode(secret, True)
msg = struct.pack(">Q", intervals_no)
h = hmac.new(key, msg, hashlib.sha1).digest()
o = ord(chr(h[19])) & 15
h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
return h
def get_totp_token(secret):
return get_hotp_token(secret, intervals_no=int(time.time())//30)
value = get_totp_token("UNRNQAU5EIFMSMJY")
print(f"{str(value).zfill(0)}")
![](https://img-blog.csdnimg.cn/d32fdfb1052e4651b97550cbf08ee304.png)
校验验证码
# -*- encoding: utf-8 -*-"
# @Module : google auth
import pyotp
def Google_Verify_Result(secret_key, verifycode):
t = pyotp.TOTP(secret_key)
result = t.verify(verifycode) # 对输入验证码进行校验,正确返回True
msg = result if result is True else False
return msg
msg = Google_Verify_Result("UNRNQAU5EIFMSMJY", "888888")
print(msg)
引用:
python3计算Google Authenticatorhttp://www.simonzhang.net/?p=3238 python 实现google authenticator 认证 - 王大拿 - 博客园python 实现google authenticator 认证 一、环境描述 windows开发环境 python:3.6 所需安装包 pyotp qrcode Image 二、实现原理 1.使用p
https://www.cnblogs.com/wangkun122/p/12582938.html