这是因为 Linux 的 glibc 处理密码的方式不同 - Linux 上密码的盐值对应于它生成的散列类型。OSX crypt() 是普通的 DES 加密,(这很可怕)。
glibc 支持多种哈希算法(MD5、Blowfish、SHA-256 等)。
如果我们查看crypt.3联机帮助页,我们可以看到:
If salt is a character string starting with the characters "$id$" followed by
a string terminated by "$":
$id$salt$encrypted
then instead of using the DES machine, id identifies the encryption method
used and this then determines how the rest of the password string is
interpreted. The following values of id are supported:
ID | Method
---------------------------------------------------------
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
因此,鉴于这些信息.. 让我们使用 Linux 的 crypt 从第二个示例中获取您的密码
$1$VFvON1xK$SboCDZGBieKF1ns2GBfY50' ('test', encrypted with salt=VFvON1xK)
1 == MD5
VFvON1xK == Salt
SboCDZGBieKF1ns2GBfY50 == Hashed password
幸运的是,有一个跨平台的解决方案passlib.hash.md5_crypt。
下面是你如何使用它:
from passlib.hash import md5_crypt
hash = md5_crypt.encrypt("test",salt="VFvON1xK")
print hash
在 Linux 或 OSX 上运行时,生成以下 glibc 友好密码哈希:
$1$VFvON1xK$SboCDZGBieKF1ns2GBfY50
与在 Linux 机器上生成的原始版本相同。