如何解决用python加密密码
我最近正在使用Pyqt5开发一个程序,并将自己构建为一个小型软件。
该软件需要用户名和密码才能登录,我想设置一次用户名和密码,以安全的方式保存它,并防止拥有我的密码\访问我的计算机的人能够登录到我的软件。
到目前为止,我所做的是使用 keyring 模块设置密码,并使用 passlib 对密码进行哈希处理-在对哈希进行哈希处理之后,该密码最终会保存我的密码。 当用户尝试登录时-代码将使用输入的密码并将其与密钥环文件中的哈希密码进行比较。
问题在于:
- 这是保存用户密码的好方法吗?安全吗?
- 我怎样才能阻止有权访问我的代码或计算机的人打开代码并查看密码?
这是密码哈希脚本:
from passlib.context import CryptContext
import keyring
# create CryptContext Object
context = CryptContext(
schemes=["pbkdf2_sha256"],default="pbkdf2_sha256",pbkdf2_sha256__default_rounds=50000
)
def password_encrypter (password):
# hash password
hashed_password = context.hash(password)
return hashed_password
def password_hiding (password):
# Gets password from user and encrypt it
hashed_password = password_encrypter(password)
# Hides The Password
keyring.set_password("service_name","user_name",hashed_password)
check_if_hashed = context.verify(password,hashed_password)
password1 = keyring.get_password("service_name","user_name")
# Just for testing
print ("password from user",password)
print ("hashed password : ",hashed_password)
print("password from keyring: ",password1)
return password1
def password_validatation (password):
hidden_password = password_hiding(password)
check_if_hashed = context.verify(password,hidden_password)
print(check_if_hashed)
return check_if_hashed
# Test
password_validatation("my_password")
我还要添加登录脚本:
from PyQt5 import QtWidgets
# from mainwindow import Ui_MainWindow
from qtwidgets import PasswordEdit
from .password_generator import password_validatation
class Login(QtWidgets.QDialog):
def __init__(self,parent=None):
super(Login,self).__init__(parent)
self.textName = QtWidgets.QLineEdit(self)
self.textPass = PasswordEdit()
self.buttonLogin = QtWidgets.QPushButton('Login',self)
self.buttonLogin.clicked.connect(self.handleLogin)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.textName)
layout.addWidget(self.textPass)
layout.addWidget(self.buttonLogin)
def handleLogin(self):
password = password_validatation(self.textPass.text())
if (self.textName.text() == 'user_name' and
self.textPass.text() == True):
self.accept()
else:
QtWidgets.QMessageBox.warning(
self,'Error','Bad user or password!')
class Window(QtWidgets.QMainWindow):
def __init__(self,parent=None):
super(Window,self).__init__(parent)
# self.ui = Ui_MainWindow()
# self.ui.setupUi(self)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
login = Login()
if login.exec_() == QtWidgets.QDialog.Accepted:
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
解决方法
使用密钥环保存的密码已经保存在用于操作系统密码的“安全存储”中。
这是保存用户密码的好方法吗?安全吗?
是的,它依赖于操作系统自身的安全性机制。
我如何阻止有权访问我的代码或计算机的人打开代码并查看密码?
如果某人可以访问您在计算机上的会话,则无论有没有代码,他们都可以访问安全存储。您的代码没有明显的安全漏洞,此时您的安全仅限于操作系统的会话访问。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。