在公司里,我们将系统的一些部分从.NET迁移到python。其中一个部分是日志/授权模块。我需要用python CryptographyManager.CompareHash Method实现。它从数据库中获取经过salt和hash处理的密码,并将其与用户输入(明文)进行比较。要做到这一点,我必须对用户输入进行加密和散列,然后使用数据库中的散列密码进行检查
数据库中的密码由Microsoft Enterprise Library.Security.Cryptography
哈希处理。详情如下:
Symmetric Crypto Providers: algorithmType="System.Security.Cryptography.AesManaged name="AES"
Hash Providers: algorithmType="System.Security.Cryptography.SHA256Managed saltEnabled="true" name="SHA256"
我发现密码是这样写的:Base64(salt + SHA256(salt + password))
。salt是散列密码的前16个字节
我试图做的是从数据库中的散列密码中获取salt,然后将下一个散列和salt用户输入(纯文本)进行比较
不幸的是,最终没有成功。散列是不同的。可能我的de/编码有问题
我的代码如下所示:
import base64
import hashlib
hash = 'EwxBhfN0fM5Puv8/z+3/L50QvdU6BHFb4XQU9xtye/mOXJ8tBPc3tIyW7dEiZrvA'
password = 'some_plain_password'
#password = password.encode()
password = base64.b64decode(password)
#hash to byte
b_hash = base64.b64decode (hash)
print(b_hash)
#get salt, first 16 bytes
salt = b_hash[:15]
salt=base64.b64encode(salt)
print(salt)
m = hashlib.sha256()
m.update(salt)
m.update(password)
print(m.hexdigest())
如果不使用示例明文和哈希来验证结果,则执行此操作有点棘手,但您的代码应该实现您在问题中列出的算法:
我想你想要这样的东西:
我的帖子不是100%的答案,而是故事的结尾。 问题在于,没有官方来源CryptographyManager.Compare/Create Hash的工作原理 我尝试了很多与Base64(salt+SHA256(salt+password))的组合,但都没有成功。 最后,我决定重写C代码。 如果有人正在寻找对密码进行盐析和散列的python代码,并比较两种散列,下面是我的工作示例:
相关问题 更多 >
编程相关推荐