50 # passlib 1.5 wants calc_checksum, 1.6 wants _calc_checksum |
50 # passlib 1.5 wants calc_checksum, 1.6 wants _calc_checksum |
51 def calc_checksum(self, secret): |
51 def calc_checksum(self, secret): |
52 return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8') |
52 return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8') |
53 _calc_checksum = calc_checksum |
53 _calc_checksum = calc_checksum |
54 |
54 |
55 _CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1']) |
55 _CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'], |
|
56 deprecated=['cubicwebmd5crypt', 'des_crypt']) |
|
57 verify_and_update = _CRYPTO_CTX.verify_and_update |
56 |
58 |
57 def crypt_password(passwd, salt=None): |
59 def crypt_password(passwd, salt=None): |
58 """return the encrypted password using the given salt or a generated one |
60 """return the encrypted password using the given salt or a generated one |
59 """ |
61 """ |
60 if salt is None: |
62 if salt is None: |
61 return _CRYPTO_CTX.encrypt(passwd) |
63 return _CRYPTO_CTX.encrypt(passwd) |
62 # empty hash, accept any password for backwards compat |
64 # empty hash, accept any password for backwards compat |
63 if salt == '': |
65 if salt == '': |
64 return salt |
66 return salt |
65 if _CRYPTO_CTX.verify(passwd, salt): |
67 try: |
66 return salt |
68 if _CRYPTO_CTX.verify(passwd, salt): |
|
69 return salt |
|
70 except ValueError: # e.g. couldn't identify hash |
|
71 pass |
67 # wrong password |
72 # wrong password |
68 return '' |
73 return '' |
69 |
74 |
70 def cartesian_product(seqin): |
75 def cartesian_product(seqin): |
71 """returns a generator which returns the cartesian product of `seqin` |
76 """returns a generator which returns the cartesian product of `seqin` |