Using the crypt module in Windows?
Asked Answered
S

6

6

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):

  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?

Swenson answered 9/11, 2013 at 15:51 Comment(1)
use cygwin for windows and make sure to select "crypt-python" when installingImpost
B
5

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

Brighton answered 9/11, 2013 at 16:21 Comment(0)
B
10

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.

Biggs answered 18/2, 2015 at 2:39 Comment(4)
Crypt is a much simpler and shorter hash method, unique to older UNIX systems, it has nothing to do with MD5/SHA and is not compatible with those. It's mostly obsolete, and certainly not secure, but if you are trying to compare to old crypt passwords, you will need to use crypt specifically, not MD5 or SHA.Nit
I really don't understand your comment. In any case, I have yet to find an easier method of generating passwords for Linux-compatible VMs while using Windows as a development environment ;) Anything else proved too much pain or was of doubtful quality/functionality. Nonetheless, this lib is crypt-compatible if you look at the docs :]Biggs
@Nit The crypt(3) function in the system’s libc can support more than just the old UNIX crypt method. In particular, glibc has a SHA-512-derived hash construction ($6$...). Then if you are generating password for /etc/password, nginx basic auth or anything else that uses crypt(3), you don’t actually have to use old UNIX crypt algorithm. Python’s crypt module supports these alternative methods since Python 3.3.Crowl
Passlib is the way to go if you need a os independet, relyable method to generate various hashes, including the supported Linux crypt variants. I didn't hear of passlib before but after some research I find it quite nice. 0 dependencies by default but will use other libs if installed or required for special hash methods. Also very nicely documented.Brahmana
B
5

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

Brighton answered 9/11, 2013 at 16:21 Comment(0)
O
2

I found an alternative module called fcrypt available here:

It's old, so don't expect python3 compatibility.

Oneal answered 14/1, 2014 at 4:47 Comment(2)
This is positively ancient and support only DES, none of the modern algorithmsKhz
You're commenting on a soon-to-be ten year-old post.Oneal
R
0

If you are on Windows, you can easily use the bcrypt module This is supported on both windows and Mac. However, if the error continues in my own case, check if the code automatically imports crypt for you.

Russom answered 24/8, 2022 at 21:22 Comment(0)
N
0

I had the same issue. I was trying to use crypt on my Windows 10. I used sha512 which fixed the issue.

from passlib.hash import sha512_crypt as sha512
hash = sha512.hash(passwd, rounds=5000)
Nympho answered 8/11, 2023 at 5:5 Comment(0)
I
-3

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

Intoxication answered 14/10, 2017 at 10:11 Comment(1)
This has so many issues I don't even know where to start. First: it doesn't create a "Linux" crypt password compatible with the shadows file ($1, $5, $6). Second: 'pass' is a reserved word, so your code won't run. Third: what exactly is the print supposed to print? And last: why do you reference passlib when you talk about bcrypt?Brahmana

© 2022 - 2024 — McMap. All rights reserved.