问题
In IDLE and Python version 3.3.2, I try and call the python module like so:
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.
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):
- Rename the directory in the
Python33
directory fromLib
tolib
- Rename the
crypt.py
inlib
to_crypt.py
. However, it turns out the entirecrypt.py
module depends on an external module called_crypt.py
too. - Browsed internet to download anything remotely appropriate to resemble
_crypt.py
It's not Python, right? 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. Or have I found a bug in 3.3.2?
回答1:
I assume that is because crypt
is a Unix Specific Service.
Right at the top of the docs for crypt
:
34.5. crypt — Function to check Unix passwords
Platforms: Unix
回答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). I've verified this by using Kickstart files by injecting the generated hashed password value in rootpw and user attributes. 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)
***NOTE: Tested with SHA512 but I see no reason why it shouldn't work with SHA256 or MD5.
回答3:
I found an alternative module called fcrypt available here:
- https://pypi.python.org/pypi/fcrypt/1.3.1
- http://carey.geek.nz/code/python-fcrypt/
It's old, so don't expect python3 compatibility.
回答4:
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. Go for 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
https://pypi.python.org/pypi/bcrypt/2.0.0
来源:https://stackoverflow.com/questions/19877867/using-the-crypt-module-in-windows