致谢声明
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