Using the crypt module in Windows?

青春壹個敷衍的年華 提交于 2019-11-29 14:50:11

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

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.

I found an alternative module called fcrypt available here:

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!