【问题标题】:Validate in python password against an Scrypt combined hash: (Settings+Salt+Hash)针对 Scrypt 组合哈希验证 python 密码:(设置+盐+哈希)
【发布时间】:2021-12-12 19:41:27
【问题描述】:

是否有任何 Python 库可以针对嵌入了设置和盐的哈希验证密码(如 Java 中的 com.lambdaworks.crypto.SCryptUtil.check(pass, hash))?

例如,pass123 应该对 $s0$e1010$Hcxkdy8NyRZt6hCnNsDyhQ==$KG8hw/i7zDbVtwtuJfUHlo1vw+qrshDYMq6XlfYW0rg= 有效

【问题讨论】:

  • 有内置的crypt 模块,但文档告诉我它不是很便携。第三方passlib 看起来应该是你想要的,虽然我没有用过。

标签: python hash bcrypt scrypt


【解决方案1】:

在 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

【讨论】:

    【解决方案2】:

    无论如何,既然 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="))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2012-03-01
      相关资源
      最近更新 更多