Is there a drop-in replacement for Python’s crypt
module for the code shown below?
The crypt
module in Python’s standard library is deprecated. When it is imported, the following warning is displayed:
DeprecationWarning: 'crypt' is deprecated and slated for removal in Python 3.13
According to the Python documentation, The passlib package can replace all use cases of this module.
Here is a first attempt at replacing crypt.crypt
one for one, but the output is different than what is generated by crypt
.
import base64
import crypt
import secrets
from passlib.hash import sha512_crypt
secret_bytes = secrets.token_bytes(60)
secret = base64.standard_b64encode(secret_bytes).decode()
pw = sha512_crypt.using(salt='').hash(secret).split('$')[-1]
print(f'passlib: $6$${pw}')
pw = crypt.crypt(secret, salt='$6$')
print(f'crypt: {pw}')
Output
passlib: $6$$hdHFHtZ76R54qDgAEh607JrTGwjhphIdb3PYVr6gxiyV1oaDXTuuX/6IfPfqUVWUEhdK3U97tgoObPrDtzeJl.
crypt: $6$$0d/bb9nahTRv6XcGU4pCx..4/rzTcfg4hIGdihTK8M1DPC4PqVzMOEgz8O9DKG14yKE4uQZQdDXC8gl3hkINM0
What code changes are needed to make the passlib output the same as the crypt output?