简体   繁体   English

使用 Windows 中的 crypt 模块?

[英]Using the crypt module in Windows?

In IDLE and Python version 3.3.2, I try and call the python module like so:在 IDLE 和 Python 版本 3.3.2 中,我尝试像这样调用 python 模块:

hash2 = crypt(word, salt)

I import it at the top of my program like so:我将它导入到我的程序顶部,如下所示:

from crypt import *

The result I get is the following:我得到的结果如下:

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'

However, when I execute the same file adams.py in Ubuntu, with Python 2.7.3, it executes perfectly - no errors.但是,当我在 Ubuntu 中使用 Python 2.7.3 执行相同的文件adams.py时,它执行完美 - 没有错误。

I tried the following to resolve the issue for my Windows & Python 3.3.2 (though I'm sure the OS isn't the issue, the Python version or my use of syntax is the issue):我尝试了以下方法来解决我的 Windows 和 Python 3.3.2 的问题(尽管我确定操作系统不是问题,ZA7F5F35426B927411FC9231B56382173 的版本是语法问题)

  1. Rename the directory in the Python33 directory from Lib to libPython33目录中的目录从Lib重命名为lib
  2. Rename the crypt.py in lib to _crypt.py .lib中的crypt.py重命名为_crypt.py However, it turns out the entire crypt.py module depends on an external module called _crypt.py too.然而,事实证明整个crypt.py模块也依赖于一个名为_crypt.py的外部模块。
  3. Browsed internet to download anything remotely appropriate to resemble _crypt.py浏览互联网以远程下载任何类似于_crypt.py的内容

It's not Python, right?不是 Python 对吧? It's me...(?) I'm using syntaxes to import and use external modules that are acceptable in 2.7.3, but not in 3.3.2.是我……(?)我正在使用语法来导入和使用在 2.7.3 中可接受但在 3.3.2 中不可接受的外部模块。 Or have I found a bug in 3.3.2?还是我在 3.3.2 中发现了一个错误?

A better approach would be to use the python passlib module which generates compatible crypt hashes of linux passwords (I assume that's what you most probably want).更好的方法是使用 python passlib 模块,该模块生成 linux 密码的兼容 crypt 哈希(我认为这是您最可能想要的)。 I've verified this by using Kickstart files by injecting the generated hashed password value in rootpw and user attributes.我已经使用 Kickstart 文件通过在 rootpw 和用户属性中注入生成的散列密码值来验证这一点。 The functions you need are:您需要的功能是:

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)

The first parameter is self-explanatory.第一个参数是不言自明的。
The second & third parameter have to do with specification compliance and are required to generate linux compatible password hashes *** (see: Passlib: SHA256 spec, format & algorithm )第二个和第三个参数与规范合规性有关,需要生成与 linux 兼容的密码哈希*** (请参阅: Passlib: SHA256 规范、格式和算法

*** NOTE: Tested with SHA512 but I see no reason why it shouldn't work with SHA256 or MD5. ***注意:已使用 SHA512 进行测试,但我认为没有理由它不能与 SHA256 或 MD5 一起使用。

I assume that is because crypt is a Unix Specific Service .我认为这是因为crypt是一个Unix 特定服务

Right at the top of the docs for crypt :就在crypt文档的顶部:

34.5. 34.5. crypt — Function to check Unix passwords crypt - 检查 Unix 密码的函数

Platforms: Unix平台:Unix

I found an alternative module called fcrypt available here:我在这里找到了一个名为 fcrypt 的替代模块:

It's old, so don't expect python3 compatibility.它很旧,所以不要指望python3兼容性。

If you are on Windows, you can easily use the bcrypt module This is supported on both windows and Mac.如果您使用的是 Windows,则可以轻松使用 bcrypt 模块 这在 windows 和 Mac 上均受支持。 However, if the error continues in my own case, check if the code automatically imports crypt for you.但是,如果在我自己的情况下错误仍然存在,请检查代码是否自动为您导入 crypt。

You can use instead 'bcrypt' for this purpose on a Windows PC, this is done because crypt is a UNIX module only so wont be compatible in windows easily.为此,您可以在 Windows PC 上使用 'bcrypt' 来代替,这是因为 crypt 只是一个 UNIX 模块,因此在 Windows 中不容易兼容。 Go for bcrypt去 bcrypt

import bcrypt
password = b"passit" #passit is the word to encrypt
pass = bcrypt.hashpw(password, bcrypt.gensalt())
print(b)

This will get your work done.这将完成您的工作。 For further reference, visit: http://passlib.readthedocs.io/en/stable/install.html如需进一步参考,请访问: http : //passlib.readthedocs.io/en/stable/install.html

https://pypi.python.org/pypi/bcrypt/2.0.0 https://pypi.python.org/pypi/bcrypt/2.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM