import bcrypt

def hash_password(password):

# 生成一个新的salt

salt = bcrypt.gensalt()

print(salt)

# 使用生成的salt哈希密码

hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt)

print(hashed_password)

# 将salt和哈希密码合并以便存储

stored_password = f"{salt.decode('utf-8')}${hashed_password.decode('utf-8')}"

print(stored_password)

return stored_password

def verify_password(input_password, stored_password):

# 从合并的字符串中提取存储的salt

salt = stored_password.split('$$')[0].encode('utf-8')

# 使用存储的salt哈希输入密码

hashed_input_password = bcrypt.hashpw(input_password.encode('utf-8'), salt)

# 直接比较哈希后的密码,而不再次哈希输入密码

return f"{salt.decode('utf-8')}${hashed_input_password.decode('utf-8')}" == stored_password

x = hash_password("xyz")

if verify_password("xyz", x):

print("Pass")

else:

print("Fail")

为了做个登录界面,校验一下用户名密码,了解了一下这个库 ,简单做客一个用户注册和登录的界面: 源代码:

from flask import request, Flask, render_template

import bcrypt

import os

savePath = os.path.join(os.getcwd(), "userInfo")

# 实例化

app = Flask(__name__)

# 这里是主页面,即第一步显示的网页,有一个对话框和搜索按钮

@app.route('/')

def mainweb():

return render_template("first.html", result="欢迎使用NAS页面")

# 设定第二步的跳转网页,methods 设定请求类型,这里可以指定一种类型,就不用判断了。主要是类型不同,获取结果的方式不同

@app.route('/login', methods=['POST'])

def login():

# post 类型抓取对话框内的内容

username = request.form.get("username", "")

passwd = request.form.get("password", "")

if username == passwd == "":

return render_template('login.html', result="欢迎")

if verifyLogin(username, passwd):

return render_template('main.html', result="......")

else:

return render_template('login.html', result="用户名或密码错误")

@app.route('/signIn', methods=['POST'])

def signIn():

# post 类型抓取对话框内的内容

username = request.form.get("username", "")

passwd = request.form.get("password", "")

if username == passwd == "":

return render_template('signIn.html', result="请输入正确的用户名和密码")

if saveLogin(username, passwd):

return render_template('login.html', result="注册成功,请登录")

else:

return render_template('signIn.html', result="用户名重复,请更换用户名重新注册")

def verifyLogin(user, passwd):

# 读取存储的用户信息

saveFile = os.path.join(savePath, user)

if os.path.isfile(saveFile):

with open(saveFile, 'r', encoding="UTF-8") as f:

savepasswd = f.read()

# 从合并的字符串中提取存储的salt

salt = savepasswd.split('||')[0].encode('utf-8')

# 使用存储的salt哈希输入密码

hash_passwd = bcrypt.hashpw(passwd.encode('utf-8'), salt)

password = f"{salt.decode('utf-8')}||{hash_passwd.decode('utf-8')}"

return password == savepasswd

else:

return False

def saveLogin(user, passwd):

# 生成一个新的salt

salt = bcrypt.gensalt()

# 使用生成的salt哈希密码

hashed_password = bcrypt.hashpw(passwd.encode('utf-8'), salt)

# 将salt和哈希密码合并以便存储

stored_password = f"{salt.decode('utf-8')}||{hashed_password.decode('utf-8')}"

# 保存用户信息

saveFile = os.path.join(savePath, user)

if os.path.isfile(saveFile):

return False

else:

if os.path.isdir(savePath):

pass

else:

os.makedirs(savePath)

with open(saveFile, 'w', encoding="UTF-8") as f:

f.write(stored_password)

return True

if __name__ == '__main__':

app.run(host="0.0.0.0", port=5000)

主要需要调用到三个HTML模板,三个模版的背景图片还没放,也就是没有优化界面,暂时先做功能,后续优化 first.html

Login Page

哈希算法 python hash 密码校验  第1张

login.html

Login Page

哈希算法 python hash 密码校验  第1张

signIn.html

Sign In Page

哈希算法 python hash 密码校验  第1张

其实就是套用一个模板,稍微修改一些参数,变成不同的模版 此脚本运行后,会先调用first.html 生成一个选择页面, 登录|注册 选择后跳转到相应页面 登录页面,会验证用户名密码是否有在本地存储,且正确无误,如果错误会显示提示。 登录上去以后,应该跳转主页面…我现在主页面暂时没有放功能…待完善… 注册页面,会验证注册的用户名是否存在,存在则显示错误提示,不存在则创建文件存储密码到本地,随后跳转到登录界面

慢慢学,慢慢看…

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。