Commit 011bb37a authored by Dom Sekotill's avatar Dom Sekotill
Browse files

added a compatible password hashing algorithm

parent 5895ad96
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
import passlib.hash

from django.contrib.auth import hashers


class SHA256CryptPasswordHasher(hashers.BasePasswordHasher):
	""" SHA256-Crypt hasher compatible with Dovecot """

	digest = passlib.hash.sha256_crypt
	iterations = 10000

	@property
	def algorithm(self):
		return '{{{0}}}'.format(self.digest.name.upper().replace('_', '-'))

	def verify(self, password, encoded):
		return self.digest.verify(password, self._get_data(encoded))

	def encode(self, password, salt, iterations=None):
		return ''.join((
			self.algorithm,
			self.digest.encrypt(password, salt=salt,
				rounds=iterations or self.iterations)
		))

	def safe_summary(self, encoded):
		details = self.digest.from_string(self._get_data(encoded))
		return {
			'algorithm': self.digest.name,
			'iterations': details.rounds,
			'salt': hashers.mask_hash(details.salt),
			'hash': hashers.mask_hash(details.checksum),
		}

	def _get_data(self, encoded):
		algorithm = self.algorithm
		assert encoded.startswith(algorithm)
		return encoded[len(algorithm):]
+6 −0
Original line number Diff line number Diff line
@@ -16,6 +16,12 @@ INSTALLED_APPS = (
    'django.contrib.contenttypes',
)


PASSWORD_HASHERS = (
	'kodo_sso.custom_auth.pass_hashers.SHA256CryptPasswordHasher',
)


MIDDLEWARE_CLASSES = (
	'django.middleware.cache.UpdateCacheMiddleware',
	'htmlmin.middleware.HtmlMinifyMiddleware',