ImportError:无法导入名称get_hexdigest

时间:2012-02-17 07:12:39

标签: python django django-1.4

当我试图在我的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

3 个答案:

答案 0 :(得分:3)

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

解决方案:

  • 使用1.3版本(目前是最后一个稳定版)
  • 自己实施get_hexdigest(可以从here
  • 进行复制)
  • 使用其他工具(没有兼容性问题)来解决您的任务

答案 1 :(得分: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.")

答案 2 :(得分:1)

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

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

具体地:

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

make_password(密码[,盐,哈希]) 以此应用程序使用的格式创建哈希密码。它需要一个强制参数:明文密码。 (可选)如果您不想使用默认值(PASSWORD_HASHERS设置的第一个条目),则可以提供salt和哈希算法。目前支持的算法是:'pbkdf2_sha256','pbkdf2_sha1','bcrypt_sha256'(参见使用带有Django的bcrypt),'bcrypt','sha1','md5','unsalted_md5'(仅用于向后兼容)和'crypt'如果你已经安装了crypt库。如果password参数为None,则返回一个不可用的密码(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))