How to re-implement the crypt.crypt function?

Cosmia Luna cosmius at gmail.com
Sat Mar 10 14:33:36 EST 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. 

'$6$ds41p/9VMA.BHH0U$yv25s7jLxTRKLDNjIvT0Qc2jbcqdFRi5.PftO3cveTvjK49JhwCarIowOfrrNPD/PpYT3n6oNDIbjAONh8RXt1'

I tried:

from hashlib import sha512
from base64 import b64encode, b64decode

salt='ds41p/9VMA.BHH0U'
pwd='123456'

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.

Thanks,
Cosmia



More information about the Python-list mailing list