Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

python - Using the crypt module in Windows?

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:
oneofyourusinessadams.py", line 10, in <module>
    from crypt import *
  File "C:Python33libcrypt.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):

  1. Rename the directory in the Python33 directory from Lib to lib
  2. Rename the crypt.py in lib to _crypt.py. However, it turns out the entire crypt.py module depends on an external module called _crypt.py too.
  3. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...