2012-02-17 18 views
2

私はosX10.7、python2.7 django1.4システムでdjangoコードを実行しようとしています。どうすればget_hexdigestを入手できますか?どこからでもダウンロードできますか?ImportError:名前をインポートできません。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 
+0

多分あなたが作成されたコードを投稿する必要がありますこのエラー。特にユーザモデルのハッキング –

+0

Djangoの設定に何か問題があります。関数はそこにあるはずです。ファイル自体を開いてみてください。 – DrTyrsa

+0

このファイルはどこにありますか? – henghonglee

答えて

3

あなたのDjangoのDEVのバージョン(1.4)を使用して、モジュールに対応するにはget_hexdigest方法は存在しません。

ソリューション:

  • 使用
  • はそれがありません(別のツールを使用して(hereからcopypastedすることができます)1.3バージョン(それは現時点で最新の安定だ)
  • は自分でget_hexdigestを実装互換性の問題)あなたの仕事を解決するために
+0

私はそれを第2の方法でやっていましたが、それは効いていましたが、それが正しい方法であるかどうか、それが他の問題を引き起こすかどうかは確かでした。あなたの答えは私をより安全に感じさせます。ありがとう! – henghonglee

1

あなたはそれを比較のためにパスワードを暗号化するために使用しています。あなたが手動で認証したい場合は

check_password(パスワード、エンコードされた) :具体的に

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

:Djangoの1.5を(?また1.4)今より良いユーティリティ関数を提供していますが判明プレーンテキストのパスワードとデータベースのハッシュされたパスワードを比較することにより、便利な関数check_password()を使用します。チェックするプレーンテキストのパスワードと照合するデータベースのユーザーのパスワードフィールドの完全な値の2つの引数をとり、一致する場合はTrueを返し、そうでない場合はFalseを返します。

make_password(パスワード〔、塩、hashers]) は、このアプリケーションによって使用されるフォーマットでハッシュされたパスワードを作成します。 1つの必須引数、すなわちプレーンテキストのパスワードが必要です。オプションで、デフォルト(PASSWORD_HASHERS設定の最初のエントリ)を使用しない場合は、saltとハッシングアルゴリズムを使用して使用できます。現在サポートされているアルゴリズムは、 'pbkdf2_sha256'、 'pbkdf2_sha1'、 'bcrypt_sha256'(bcryptのDjangoの使用を参照)、 'bcrypt'、 'sha1'、 'md5'、 'unsalted_md5'(下位互換性の場合のみ)、 'crypt'暗号ライブラリがインストールされています。 password引数がNoneの場合、使用できないパスワード(check_password()で決して受け入れられないパスワード)が返されます。

is_password_usable(ENCODED_PASSWORD) チェックし、指定された文字列は)(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)) 
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.") 
関連する問題