|
发表于 2024-10-8 22:05:49
|
显示全部楼层
老师好!在38讲解课后作业使用:hashlib.md5(bytes(password, "utf-8"))存储密码正常的,为什么用了函数之后,同样的字符串传入后哈希值不一样,但是使用hashlib.md5(bytes(password, "utf-8")).hexdigest()存储又是正常的,麻烦解答一下,谢谢!
- import hashlib
- print("欢迎来到鱼C论坛~")
- print("=" * 50)
- user = {}
- def get_int():
- print("1. 注册")
- print("2. 登录")
- print("3. 退出")
- command = input("请输入指令:")
- print("=" * 50)
- match command:
- case "1":
- register()
-
- case "2":
- login()
- case "3":
- return 1
- def register():
- name = input("请输入用户名:")
- while name in user:
- print("用户名已注册!")
- name = input("请重新输入用户名: ")
- password = input("请输入密码:")
- while not password:
- print("密码为空,请重新输入!")
- password = encrypt(password)
- user[name] = password
- print(f"注册时的密码:{user[name]}")
- print("恭喜,注册成功~")
- print("=" * 50)
-
- def encrypt(password):
- hash_password = hashlib.md5(bytes(password, "utf-8"))
- print(f"原始密码 = {password}, 哈希密码 = {hash_password}, 密码长度 = {len(password)}")
- return hash_password
-
- def login():
- name = input("请输入用户名:")
- while name not in user:
- print("该用户名不存在!")
- name = input("请重新输入用户名:")
-
- password =input("请输入密码:")
- password = encrypt(password)
- print(f"登陆的哈希密码:{password}")
- while user[name] != password:
- print("密码错误!")
- password = input("请重新输入密码:")
- password = encrypt(password)
- else:
- print("恭喜,登录成功~")
- print("=" * 50)
-
-
- while True:
- if get_int():
- break
-
复制代码 |
-
登录和注册输入密码一样,哈希值不一样
-
原代码
|