基于python+PyQt5的Google身份验证器

致谢声明

1.本文学习nanhuier的博客《Python计算谷歌身份验证器的验证码》并优化其中代码。
原博客链接:https://blog.csdn.net/nanhuier/article/details/77679200
2.本文学习莫水千流的博客《程序员之路:python3+PyQt5+pycharm桌面GUI开发》,
成功搭建PyQt5+Pycharm的开发环境,建议读者先按照此文配置好环境。
原博客链接:https://www.cnblogs.com/zhoug2020/p/9039993.html
3.本文学习maicss的github工程《PyQt5-Chinese-tutorial》
github链接:https://github.com/maicss/PyQt5-Chinese-tutorial
4.本文学习晴空行的博客《Python打包方法》,
原博客链接:https://www.cnblogs.com/gopythoner/p/6337543.html

0.完整代码

读者安装PyQt5库后可以直接运行此段代码查看程序效果。
安装PyQt5库命令:pip install pyqt5
本文的第1节《定义函数getGoogleCode》、第2节《定义类Ui_Form》、第3节《定义程序入口,即主函数main》是本节《完整代码》的分段讲解。

import base64, time, struct, hmac, hashlib
def getGoogleCode(secretKey):
    decoded_secretKey = base64.b32decode(secretKey, True)
    interval_number = int(time.time() // 30)
    message = struct.pack(">Q", interval_number)
    digest = hmac.new(decoded_secretKey, message, hashlib.sha1).digest()
    index = ord(chr(digest[19])) % 16
    googleCode = (struct.unpack(">I", digest[index:index+4])[0] & 0x7fffffff) % 1000000
    return "%06d" %googleCode

from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton
from PyQt5.QtCore import QCoreApplication, QTimer, QRect, QMetaObject
class Ui_Form(object):
    def setupUi(self, Form):
        #Form setting
        Form.setObjectName("googleAuthenticator")
        Form.resize(450, 350)
        #lineEdit_secretKey setting
        self.lineEdit_secretKey = QLineEdit(Form)
        self.lineEdit_secretKey.setGeometry(QRect(180, 40, 190, 40))
        font = QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.lineEdit_secretKey.setFont(font)
        self.lineEdit_secretKey.setObjectName("lineEdit_secretKey")
        #label setting
        self.label 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值