import hashlib

from django.contrib.auth.hashers import BasePasswordHasher
from django.contrib.auth.hashers import mask_hash
from django.utils.crypto import constant_time_compare
from django.utils.datastructures import SortedDict


class Sha256Hasher(BasePasswordHasher):
    """
    SHA-256 password hasher.
    
    """
    algorithm = 'sha256'
    digest = hashlib.sha256

    def encode(self, password, salt):
        assert password
        assert salt and '$' not in salt
        hash = self.digest(salt + password).hexdigest()
        return "%s$%s$%s" % (self.algorithm, salt, hash)

    def verify(self, password, encoded):
        algorithm, salt, hash = encoded.split('$', 2)
        assert algorithm == self.algorithm
        encoded_2 = self.encode(password, salt)
        return constant_time_compare(encoded, encoded_2)

    def safe_summary(self, encoded):
        algorithm, salt, hash = encoded.split('$', 2)
        assert algorithm == self.algorithm
        return SortedDict([
            (_('algorithm'), algorithm),
            (_('salt'), mask_hash(salt, show=2)),
            (_('hash'), mask_hash(hash)),
        ])

@@ -224,16 +224,73 @@
 edDict%0A%0A
+from tower import ugettext as _, ugettext_lazy as _lazy%0A%0A
 %0Aclass S

fix bug 857566: Ensure _() imported in user/backends.py