python3 实现 google authenticator 认证/校验

生成验证码

# -*- 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)}")

Google Auth 测试用二维码

校验验证码

# -*- 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.使用phttps://www.cnblogs.com/wangkun122/p/12582938.html

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Google Authenticator(谷歌验证)的密码输入,实际上并没有一个固定的密码。Google Authenticator通过基于时间的一次性密码(TOTP)生成密码来进行账户验证。这种密码是根据事先与服务约定好的密钥和当前的时间戳来生成的。当用户登录时,他们需要在Google Authenticator应用程序中输入由此算法生成的一次性密码,并将其发送到服务进行验证。如果生成的一次性密码与服务计算的密码一致,登录将成功。这种基于时间的算法确保了每个一次性密码只有在短时间内有效,提高了账户的安全性。所以实际上,你需要根据服务提供的密钥和当前的时间戳,在Google Authenticator应用程序中生成对应的一次性密码,并输入该密码进行验证。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [谷歌验证 Google Authenticator工作原理](https://blog.csdn.net/weixin_39732991/article/details/110784762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [谷歌Google authenticator 整合到JAVA项目](https://blog.csdn.net/baidu_38990811/article/details/106002098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值