Python Usage of Crypt Function

python crypt.crypt method
modulenotfounderror: no module named '_crypt'
python crypt.crypt decrypt
attributeerror: 'module' object has no attribute 'mksalt'
fcrypt python
des-based crypt function
python encrypt password
crypt library

How to implement the crypt option in Python 3?

I understand that the use is:

Hash = crypt.crypt(password, salt)

However, the function has a set of different hashing functions. From the documentation:

crypt.METHOD_SHA512 A Modular Crypt Format method with 16 character salt and 86 character hash. This is the strongest method.

crypt.METHOD_SHA256 Another Modular Crypt Format method with 16 character salt and 43 character hash.

crypt.METHOD_MD5 Another Modular Crypt Format method with 8 character salt and 22 character hash.

crypt.METHOD_CRYPT The traditional method with a 2 character salt and 13 characters of hash. This is the weakest method.

Again my question is how do I chose which algorithm the function uses?

This is a very basic question I can't believe that I failed to find the answer on my own - I apologise if I'm wasting your time.


You pass the method as the salt argument. From the crypt function docstring:

If salt is not specified or is None, the strongest available method will be selected and a salt generated. Otherwise, salt may be one of the crypt.METHOD_* values, or a string as returned by crypt.mksalt().

For example:

crypt.crypt("password", crypt.METHOD_SHA512)

Which under the hood becomes:

crypt.crypt("password", crypt.mksalt(crypt.METHOD_SHA512))

crypt — Function to check Unix passwords, Possible uses include storing hashed passwords so you can check A Modular Crypt Format method with 16 character salt and 86 character hash based on the  The crypt module defines the following functions: crypt. crypt (word, salt=None) ¶ word will usually be a user’s password as typed at a prompt or in a graphical interface.


using bash:$ python3 -c "import crypt; print(crypt.crypt('password', '\$6\$saltstring'))"

$6$ is for SHA512 and $ must be escaped by \ if you use a bash terminal like me

36.5. crypt — Function to check Unix passwords, Possible uses include allowing Python scripts to accept typed passwords from the user, or attempting to crack Unix passwords with a dictionary. Notice that the  The following are code examples for showing how to use crypt.crypt().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.


Adapted from a Red Hat solution and @Leonard_Saracini answer (remove the backslash to make it a oneliner):

python3 -c 'import crypt,getpass,sys; \
print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))'

This is should be secure as of August 2018.

Python Usage of Crypt Function, You pass the method as the salt argument. From the crypt function docstring: If salt is not specified or is None , the strongest available method  The following scripts demonstrate the process of Python cryptography and hashing and how to use ‘Crypt’ and ‘hashlib’ module ‘Crypt’ - a function to check Unix passwords. This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm.


The crypt module, Unix systems use this encryption algorithm to store passwords, and this module is really only useful File: crypt-example-1.py import crypt import random, string def getsalt(chars = string.letters + python crypt-example-1.py 'py8UGrijma1j6'. crypt.crypt() needs two chars to use as a salt. You can either pass it a two char string OR use the previous output of the function. (crypt.crypt() returns a string with first two chars being the salt and the rest being the hash) I looked at https://docs.python.org/3.4/library/crypt.html to answer this.


crypt.crypt Python Example, This page provides Python code examples for crypt.crypt. The following are code examples for showing how to use crypt.crypt(). def test_passInCheckers(​self): """ L{UNIXPasswordDatabase} takes a list of functions to check for UNIX user  crypt — Function to check Unix passwords¶ Platforms: Unix. This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further details. Possible uses include allowing Python scripts to accept typed passwords from the user, or attempting to crack Unix passwords with a dictionary.


The crypt Module - Python Standard Library [Book], Unix systems use this encryption algorithm to store passwords, and this module is really only useful to generate or check such passwords. Example 2-40 shows  crypt — Function to check Unix passwords¶ This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further details. Possible uses include allowing Python scripts to accept typed passwords from the user, or attempting to crack Unix passwords with a dictionary.