server/utils.py
changeset 8425 b86bdc343c18
parent 8399 0ea4ccf1d9a6
parent 8414 e7243ed7bb0a
child 8446 cae198371548
equal deleted inserted replaced
8412:09432a572a44 8425:b86bdc343c18
    37     max_salt_size = 8
    37     max_salt_size = 8
    38     salt_chars = uh.H64_CHARS
    38     salt_chars = uh.H64_CHARS
    39 
    39 
    40     @classmethod
    40     @classmethod
    41     def from_string(cls, hash):
    41     def from_string(cls, hash):
    42         if hash is None:
    42         salt, chk = uh.parse_mc2(hash, u'')
    43             raise ValueError("no hash specified")
    43         if chk is None:
    44         if hash.count('$') != 1:
    44             raise ValueError('missing checksum')
    45             raise ValueError("invalid cubicweb-md5 hash")
    45         return cls(salt=salt, checksum=chk)
    46         salt = hash.split('$', 1)[0]
       
    47         chk = hash.split('$', 1)[1]
       
    48         return cls(salt=salt, checksum=chk, strict=True)
       
    49 
    46 
    50     def to_string(self):
    47     def to_string(self):
    51         return to_hash_str(u'%s$%s' % (self.salt, self.checksum or u''))
    48         return to_hash_str(u'%s$%s' % (self.salt, self.checksum or u''))
    52 
    49 
       
    50     # passlib 1.5 wants calc_checksum, 1.6 wants _calc_checksum
    53     def calc_checksum(self, secret):
    51     def calc_checksum(self, secret):
    54         return md5crypt(secret, self.salt.encode('ascii'))
    52         return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8')
       
    53     _calc_checksum = calc_checksum
    55 
    54 
    56 _CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'])
    55 _CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'])
    57 
    56 
    58 def crypt_password(passwd, salt=None):
    57 def crypt_password(passwd, salt=None):
    59     """return the encrypted password using the given salt or a generated one
    58     """return the encrypted password using the given salt or a generated one