我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用django.contrib.auth.hashers.get_hasher()。
def identify_hasher(self, encoded): """ Passlib replacement for identify_hasher() -- Identify django hasher based on hash. """ handler = self.context.identify(encoded, resolve=True, required=True) if handler.name == "django_salted_sha1" and encoded.startswith("sha1$$"): # Django uses a separate hasher for "sha1$$digest" hashes, but # passlib identifies it as belonging to "sha1$salt$digest" handler. # We want to resolve to correct django hasher. return self.get_hasher("unsalted_sha1") return self.passlib_to_django(handler) #============================================================================= # django.contrib.auth.hashers helpers -- password helpers #=============================================================================
def _create_django_hasher(self, django_name): """ helper to create new django hasher by name. wraps underlying django methods. """ # if we haven't patched django, can use it directly module = sys.modules.get("passlib.ext.django.models") if module is None or not module.adapter.patched: from django.contrib.auth.hashers import get_hasher return get_hasher(django_name) # We've patched django's get_hashers(), so calling django's get_hasher() # or get_hashers_by_algorithm() would only land us back here. # As non-ideal workaround, have to use original get_hashers(), get_hashers = module.adapter._manager.getorig("django.contrib.auth.hashers:get_hashers").__wrapped__ for hasher in get_hashers(): if hasher.algorithm == django_name: return hasher # hardcode a few for cases where get_hashers() look won't work. path = self._builtin_django_hashers.get(django_name) if path: if "." not in path: path = "django.contrib.auth.hashers." + path from django.utils.module_loading import import_string return import_string(path)() raise ValueError("unknown hasher: %r" % django_name) #============================================================================= # reverse django -> passlib #=============================================================================
def get_hasher(self, algorithm="default"): """ Passlib replacement for get_hasher() -- Return django hasher by name """ return self.resolve_django_hasher(algorithm)
def check(self, user, password): amount = int(get_channel_setting(user.channel.slug, "CANT_REUSE_LAST_PASSWORDS")[0]) if amount: history = PasswordHistory.objects.filter(user=user).order_by('-id')[:amount] for item in history: algorithm = item.hashed_password.split("$")[0] hasher = get_hasher(algorithm=algorithm) matches = hasher.verify(password, item.hashed_password) if matches: raise serializers.ValidationError("You cannot reuse the last {} used passwords.".format(amount))