def hexdigest(self, password):
if self.algorithm == PasswordEncryption.ALGORITHM_CRYPT:
try:
import crypt
except ImportError:
self.error("crypt module not found in this system. Please use md5 or sha* algorithm")
return crypt.crypt(password, self.salt)
encoded_str = (self.salt + password).encode('utf-8')
if self.algorithm == PasswordEncryption.ALGORITHM_SHA1:
return hashlib.sha1(encoded_str).hexdigest()
elif self.algorithm == PasswordEncryption.ALGORITHM_MD5:
return hashlib.md5(encoded_str).hexdigest()
elif self.algorithm == PasswordEncryption.ALGORITHM_SHA256:
return hashlib.sha256(encoded_str).hexdigest()
elif self.algorithm == PasswordEncryption.ALGORITHM_SHA512:
return hashlib.sha512(encoded_str).hexdigest()
raise ValueError('Unsupported hash type %s' % self.algorithm)
评论列表
文章目录