2

当我尝试在我的 osX10.7、python2.7 django1.4 系统上运行一段 django 代码时,我遇到了这个问题。我如何获得 get_hexdigest?我从某个地方下载它吗?

Kinnovates-MacBook-Pro:platformsite Kinnovate$ sudo python manage.py runserver
Running in development mode.
Running in development mode.
Running in development mode.
Running in development mode.
Validating models...

HACKUING USER MODEL
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1016bc050>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errors
    self._populate()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 67, in _populate
    self.load_app(app_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/models.py", line 2, in <module>
    from django_sha2 import auth
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/auth.py", line 96, in <module>
    monkeypatch()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/auth.py", line 42, in monkeypatch
    from django_sha2 import bcrypt_auth
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/bcrypt_auth.py", line 10, in <module>
    from django.contrib.auth.models import get_hexdigest
ImportError: cannot import name get_hexdigest
4

3 回答 3

3

您正在使用 Django (1.4) 的开发版本,并且相应模块中没有get_hexdigest方法。

解决方案:

  • 使用 1.3 版本(这是目前最后一个稳定版本)
  • 自己实现get_hexdigest(可以从这里复制粘贴)
  • 使用另一个工具(没有兼容性问题)来解决您的任务
于 2012-02-17T09:58:44.570 回答
2

您很可能正在使用它来加密密码以进行比较。原来 Django 1.5 ( Also 1.4 ? ) 现在提供了更好的实用功能:

https://docs.djangoproject.com/en/dev/topics/auth/passwords/#auth-password-storage

具体来说:

check_password(password, encoded) 如果您想通过将纯文本密码与数据库中的散列密码进行比较来手动验证用户身份,请使用便捷函数 check_password()。它有两个参数:要检查的纯文本密码,以及要检查的数据库中用户密码字段的完整值,如果匹配则返回 True,否则返回 False。

make_password(password[, salt, hashers]) 以该应用程序使用的格式创建一个散列密码。它需要一个强制性参数:纯文本密码。或者,如果您不想使用默认值(PASSWORD_HASHERS 设置的第一个条目),您可以提供要使用的盐和散列算法。当前支持的算法有:'pbkdf2_sha256'、'pbkdf2_sha1'、'bcrypt_sha256'(参见 Using bcrypt with Django)、'bcrypt'、'sha1'、'md5'、'unsalted_md5'(仅用于向后兼容)和 'crypt' if你已经安装了 crypt 库。如果密码参数为无,则返回不可用的密码(check_password() 永远不会接受的密码)。

is_password_usable(encoded_pa​​ssword) 检查给定的字符串是否是一个散列密码,有机会通过 check_password() 进行验证。

旧代码

def check_master_password(raw_password):
  from django.conf import settings
  from django.contrib.auth.models import get_hexdigest

  enc_password = getattr(settings, 'MASTER_PASSWORD', None)
  if enc_password:
    algo, salt, hsh = enc_password.split('$')
    return hsh == get_hexdigest(algo, salt, raw_password)

新的 1.5 代码

def check_master_password(raw_password):
  from django.conf import settings
  from django.contrib.auth.hashers import check_password
  return check_password(raw_password, getattr(settings, 'MASTER_PASSWORD', None))
于 2013-06-18T18:31:00.627 回答
2

自己实现该方法(使用hashlib代替hashcompat):

import hashlib
from django.utils.encoding import smart_str


def get_hexdigest(algorithm, salt, raw_password):
    """
    Returns a string of the hexdigest of the given plaintext password and salt
    using the given algorithm ('md5', 'sha1' or 'crypt').
    """
    raw_password, salt = smart_str(raw_password), smart_str(salt)
    if algorithm == 'crypt':
        try:
            import crypt
        except ImportError:
            raise ValueError('"crypt" password algorithm not supported in this environment')
        return crypt.crypt(raw_password, salt)

    if algorithm == 'md5':
        return hashlib.md5(salt + raw_password).hexdigest()
    elif algorithm == 'sha1':
        return hashlib.sha1(salt + raw_password).hexdigest()
    raise ValueError("Got unknown password algorithm type in password.")
于 2014-01-06T17:26:43.907 回答