[英]Using the crypt module in Windows?
在 IDLE 和 Python 版本 3.3.2 中,我尝试像这样调用 python 模块:
hash2 = crypt(word, salt)
我将它导入到我的程序顶部,如下所示:
from crypt import *
我得到的结果如下:
Traceback (most recent call last):
File "C:\none\of\your\business\adams.py", line 10, in <module>
from crypt import *
File "C:\Python33\lib\crypt.py", line 3, in <module>
import _crypt
ImportError: No module named '_crypt'
但是,当我在 Ubuntu 中使用 Python 2.7.3 执行相同的文件adams.py
时,它执行完美 - 没有错误。
我尝试了以下方法来解决我的 Windows 和 Python 3.3.2 的问题(尽管我确定操作系统不是问题,ZA7F5F35426B927411FC9231B56382173 的版本是语法问题)
Python33
目录中的目录从Lib
重命名为lib
lib
中的crypt.py
重命名为_crypt.py
。 然而,事实证明整个crypt.py
模块也依赖于一个名为_crypt.py
的外部模块。_crypt.py
的内容不是 Python 对吧? 是我……(?)我正在使用语法来导入和使用在 2.7.3 中可接受但在 3.3.2 中不可接受的外部模块。 还是我在 3.3.2 中发现了一个错误?
更好的方法是使用 python passlib 模块,该模块生成 linux 密码的兼容 crypt 哈希(我认为这是您最可能想要的)。 我已经使用 Kickstart 文件通过在 rootpw 和用户属性中注入生成的散列密码值来验证这一点。 您需要的功能是:
from passlib.hash import md5_crypt as md5
from passlib.hash import sha256_crypt as sha256
from passlib.hash import sha512_crypt as sha512
md5_passwd = md5.encrypt(passwd, rounds=5000, implicit_rounds=True)
sha256_passwd = sha256.encrypt(passwd, rounds=5000, implicit_rounds=True)
sha512_passwd = sha512.encrypt(passwd, rounds=5000, implicit_rounds=True)
第一个参数是不言自明的。
第二个和第三个参数与规范合规性有关,需要生成与 linux 兼容的密码哈希*** (请参阅: Passlib: SHA256 规范、格式和算法)
***注意:已使用 SHA512 进行测试,但我认为没有理由它不能与 SHA256 或 MD5 一起使用。
我在这里找到了一个名为 fcrypt 的替代模块:
它很旧,所以不要指望python3兼容性。
如果您使用的是 Windows,则可以轻松使用 bcrypt 模块 这在 windows 和 Mac 上均受支持。 但是,如果在我自己的情况下错误仍然存在,请检查代码是否自动为您导入 crypt。
为此,您可以在 Windows PC 上使用 'bcrypt' 来代替,这是因为 crypt 只是一个 UNIX 模块,因此在 Windows 中不容易兼容。 去 bcrypt
import bcrypt
password = b"passit" #passit is the word to encrypt
pass = bcrypt.hashpw(password, bcrypt.gensalt())
print(b)
这将完成您的工作。 如需进一步参考,请访问: http : //passlib.readthedocs.io/en/stable/install.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.