crypt — Function to check Unix passwords

Source code:Lib/crypt.py

Deprecated since version 3.11, will be removed in version 3.13:Thecrypt module is deprecated(seePEP 594 for details and alternatives).Thehashlib module is a potential replacement for certain use cases.Thepasslib package can replace all use cases of this module.


This module implements an interface to thecrypt(3) routine, which isa one-way hash function based upon a modified DES algorithm; see the Unix manpage for further details. Possible uses include storing hashed passwordsso you can check passwords without storing the actual password, or attemptingto crack Unix passwords with a dictionary.

Notice that the behavior of this module depends on the actual implementation ofthecrypt(3) routine in the running system. Therefore, anyextensions available on the current implementation will also be available onthis module.

Διαθεσιμότητα: Unix, not VxWorks.

Διαθεσιμότητα: not Emscripten, not WASI.

This module does not work or is not available on WebAssembly platformswasm32-emscripten andwasm32-wasi. SeeWebAssembly platforms for more information.

Hashing Methods

Added in version 3.3.

Thecrypt module defines the list of hashing methods (not all methodsare available on all platforms):

crypt.METHOD_SHA512

A Modular Crypt Format method with 16 character salt and 86 characterhash based on the SHA-512 hash function. This is the strongest method.

crypt.METHOD_SHA256

Another Modular Crypt Format method with 16 character salt and 43character hash based on the SHA-256 hash function.

crypt.METHOD_BLOWFISH

Another Modular Crypt Format method with 22 character salt and 31character hash based on the Blowfish cipher.

Added in version 3.7.

crypt.METHOD_MD5

Another Modular Crypt Format method with 8 character salt and 22character hash based on the MD5 hash function.

crypt.METHOD_CRYPT

The traditional method with a 2 character salt and 13 characters ofhash. This is the weakest method.

Module Attributes

Added in version 3.3.

crypt.methods

A list of available password hashing algorithms, ascrypt.METHOD_* objects. This list is sorted from strongest toweakest.

Module Functions

Thecrypt 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 graphicalinterface. The optionalsalt is either a string as returned frommksalt(), one of thecrypt.METHOD_* values (though not allmay be available on all platforms), or a full encrypted passwordincluding salt, as returned by this function. Ifsalt is notprovided, the strongest method available inmethods will be used.

Checking a password is usually done by passing the plain-text passwordasword and the full results of a previouscrypt() call,which should be the same as the results of this call.

salt (either a random 2 or 16 character string, possibly prefixed with$digit$ to indicate the method) which will be used to perturb theencryption algorithm. The characters insalt must be in the set[./a-zA-Z0-9], with the exception of Modular Crypt Format whichprefixes a$digit$.

Returns the hashed password as a string, which will be composed ofcharacters from the same alphabet as the salt.

Since a fewcrypt(3) extensions allow different values, withdifferent sizes in thesalt, it is recommended to use the full cryptedpassword as salt when checking for a password.

Άλλαξε στην έκδοση 3.3:Acceptcrypt.METHOD_* values in addition to strings forsalt.

crypt.mksalt(method=None,*,rounds=None)

Return a randomly generated salt of the specified method. If nomethod is given, the strongest method available inmethods isused.

The return value is a string suitable for passing as thesalt argumenttocrypt().

rounds specifies the number of rounds forMETHOD_SHA256,METHOD_SHA512 andMETHOD_BLOWFISH.ForMETHOD_SHA256 andMETHOD_SHA512 it must be an integer between1000 and999_999_999, the default is5000. ForMETHOD_BLOWFISH it must be a power of two between16 (24)and2_147_483_648 (231), the default is4096(212).

Added in version 3.3.

Άλλαξε στην έκδοση 3.7:Added therounds parameter.

Examples

A simple example illustrating typical use (a constant-time comparisonoperation is needed to limit exposure to timing attacks.hmac.compare_digest() is suitable for this purpose):

importpwdimportcryptimportgetpassfromhmacimportcompare_digestascompare_hashdeflogin():username=input('Python login: ')cryptedpasswd=pwd.getpwnam(username)[1]ifcryptedpasswd:ifcryptedpasswd=='x'orcryptedpasswd=='*':raiseValueError('no support for shadow passwords')cleartext=getpass.getpass()returncompare_hash(crypt.crypt(cleartext,cryptedpasswd),cryptedpasswd)else:returnTrue

To generate a hash of a password using the strongest available method andcheck it against the original:

importcryptfromhmacimportcompare_digestascompare_hashhashed=crypt.crypt(plaintext)ifnotcompare_hash(hashed,crypt.crypt(plaintext,hashed)):raiseValueError("hashed version doesn't validate against original")