【发布时间】:2021-12-12 19:41:27
【问题描述】:
是否有任何 Python 库可以针对嵌入了设置和盐的哈希验证密码(如 Java 中的 com.lambdaworks.crypto.SCryptUtil.check(pass, hash))?
例如,pass123 应该对 $s0$e1010$Hcxkdy8NyRZt6hCnNsDyhQ==$KG8hw/i7zDbVtwtuJfUHlo1vw+qrshDYMq6XlfYW0rg= 有效
【问题讨论】:
是否有任何 Python 库可以针对嵌入了设置和盐的哈希验证密码(如 Java 中的 com.lambdaworks.crypto.SCryptUtil.check(pass, hash))?
例如,pass123 应该对 $s0$e1010$Hcxkdy8NyRZt6hCnNsDyhQ==$KG8hw/i7zDbVtwtuJfUHlo1vw+qrshDYMq6XlfYW0rg= 有效
【问题讨论】:
在 Python 中,我们使用 hashlib 来处理哈希。您必须编写一些逻辑来设置一个函数,根据您选择的哈希检查给定的密码列表。
import hashlib
def pass_match(password,password_check):
foo = hashlib.sha_256()
foo.update(password)
if foo.retrieve() == password_check:
return true
return false
这当然是一个非常糟糕的实现,意味着作为一个代码示例。我把清理它作为家庭作业留给你!在此处阅读有关 hashlib 的更多信息: https://docs.python.org/3/library/hashlib.html
【讨论】:
无论如何,既然 scrypt here 建议 this,我最终是这样实现的:
import scrypt
import base64
import math
def verify_password(password, password_check):
parts = password_check.split("$")
params = int(parts[2], 16)
N = int(math.pow(2.0, float((params >> 16 & 65535))))
r = int(params >> 8 & 255)
p = int(params & 255)
salt = base64.b64decode(parts[3])
decoded_hash = base64.b64decode(parts[4])
return decoded_hash == scrypt.hash(password, salt=salt, N=N, r=r, p=p, buflen=32)
print(verify_password("pass123", "$s0$e1010$Hcxkdy8NyRZt6hCnNsDyhQ==$KG8hw/i7zDbVtwtuJfUHlo1vw+qrshDYMq6XlfYW0rg="))
【讨论】: