Skip to main content
 首页 » 编程设计

python之在 .NET 中验证 Python Passlib 生成的 PBKDF2 SHA512 哈希

2025年04月02日12kevingrace

我正在迁移一个使用 Passlib 1.6.2 的平台生成密码哈希。加密密码的代码是(以轮次的默认值调用散列):

from passlib.hash import pbkdf2_sha512 as pb 
 
def hash(cleartext, rounds=10001): 
    return pb.encrypt(cleartext, rounds=rounds) 

输出格式如下(密码“Patient3”(无引号)):

$pbkdf2-sha512$10001$0dr7v7eWUmptrfW.9z6HkA$w9j9AMVmKAP17OosCqDxDv2hjsvzlLpF8Rra8I7p/b5746rghZ8WrgEjDpvXG5hLz1UeNLzgFa81Drbx2b7.hg 

和“Testing123”

$pbkdf2-sha512$10001$2ZuTslYKAYDQGiPkfA.B8A$ChsEXEjanEToQcPJiuVaKk0Ls3n0YK7gnxsu59rxWOawl/iKgo0XSWyaAfhFV0.Yu3QqfehB4dc7yGGsIW.ARQ 

我可以看到代表:

  • 算法 SHA512
  • 迭代次数 10001
  • 盐 0dr7v7eWUmptrfW.9z6HkA(可能)

Passlib 算法定义于 their site并阅读:

All of the pbkdf2 hashes defined by passlib follow the same format, $pbkdf2-digest$rounds$salt$checksum.

$pbkdf2-digest$ is used as the Modular Crypt Format identifier ($pbkdf2-sha256$ in the example). digest - this specifies the particular cryptographic hash used in conjunction with HMAC to form PBKDF2’s pseudorandom function for that particular hash (sha256 in the example). rounds - the number of iterations that should be performed. this is encoded as a positive decimal number with no zero-padding (6400 in the example). salt - this is the adapted base64 encoding of the raw salt bytes passed into the PBKDF2 function. checksum - this is the adapted base64 encoding of the raw derived key bytes returned from the PBKDF2 function. Each scheme uses the digest size of its specific hash algorithm (digest) as the size of the raw derived key. This is enlarged by approximately 4/3 by the base64 encoding, resulting in a checksum size of 27, 43, and 86 for each of the respective algorithms listed above.

我找到了 passlib.net它看起来有点像废弃的测试版,它使用“$6$”作为算法。我无法让它验证密码。我尝试将算法更改为 $6$,但我怀疑这实际上也改变了盐。

我也试过使用 PWDTK具有不同的 salt 和 hash 值,但可能是我错误地拆分了影子密码,或者在某些我不应该出现的地方提供了 $。

有什么方法可以根据 .NET 中的哈希值验证密码吗?或者不涉及 Python 代理或让用户重新提供密码的另一种解决方案?

请您参考如下方法:

哈希通过将密码传递给 PBKDF HMAC-SHA-256 哈希方法进行验证,然后将生成的哈希与保存的哈希部分进行比较,从 Base64 版本转换回来。

将散列保存为二进制,然后分离散列 使用UTF-8编码将密码转换为二进制 PBKDF2、HMAC、SHA-256(toBinary(密码、盐、10001)==散列 密码:“患者3”

$pbkdf2 - sha512$10001$0dr7v7eWUmptrfW.9z6HkA$w9j9AMVmKAP17OosCqDxDv2hjsvzlLpF8Rra8I7p/b5746rghZ8WrgEjDpvXG5hLz1UeNLzgFa81Drbx2b7.hg

分解为(将字符串转换为标准 Base64(将 '.' 更改为 '+' 并添加尾随 '=' 填充:

pbkdf2 - sha512 
10001 
0dr7v7eWUmptrfW+9z6HkA== 
w9j9AMVmKAP17OosCqDxDv2hjsvzlLpF8Rra8I7p/b5746rghZ8WrgEjDpvXG5hLz1UeNLzgFa81Drbx2b7+hg== 

解码为十六进制:

D1DAFBBFB796526A6DADF5BEF73E8790 
C3D8FD00C5662803F5ECEA2C0AA0F10EFDA18ECBF394BA45F11ADAF08EE9FDBE7BE3AAE0859F16AE01230E9BD71B984BCF551E34BCE015AF350EB6F1D9BEFE86 

这是有道理的:16 字节(128 位)盐和 64 字节(512 位)SHA-512 哈希。

使用 UTF-8 将“Patient3”转换为二进制数组 将盐从修改后的 BASE64 编码转换为 16 字节二进制数组 使用迭代计数 od 10001 使用带有 SHA-512 的 HMAC 将其提供给 PBKDF2

我明白了

C3D8FD00C5662803F5ECEA2C0AA0F10EFDA18ECBF394BA45F11ADAF08EE9FDBE7BE3AAE0859F16AE01230E9BD71B984BCF551E34BCE015AF350EB6F1D9BEFE86 

当 Base64 编码时,将 '+' 字符替换为 '.'并剥离尾随的“=”字符返回: w9j9AMVmKAP17OosCqDxDv2hjsvzlLpF8Rra8I7p/b5746rghZ8WrgEjDpvXG5hLz1UeNLzgFa81Drbx2b7.hg