How to re-implement the crypt.crypt function?

Cosmia Luna cosmius at
Sat Mar 10 20:33:36 CET 2012

I'm not searching for a full solution and only want to know how to use hashlib to create a equivalent string like 

crypt.crypt('123456', '$6$ds41p/9VMA.BHH0U') returns the string below. 


I tried:

from hashlib import sha512
from base64 import b64encode, b64decode


b64encode( sha512(pwd+salt).digest(), altchars='./' )
b64encode( sha512(salt+pwd).digest(), altchars='./' )
b64encode( sha512( pwd + b64decode(salt, altchars='./') ).digest(), altchars='./')
b64encode( sha512( b64decode(salt, altchars='./') + pwd ).digest(), altchars='./')

of course none of the four returns the value I want, 'yv25s7jLxTRKLDNjIvT0Qc2jbcqdFRi5.PftO3cveTvjK49JhwCarIowOfrrNPD/PpYT3n6oNDIbjAONh8RXt1', how can I get the value? I can't use crypt.crypt because of the consideration of cross-platform.


More information about the Python-list mailing list