python django unsalted md5密码哈希格式

时间:2014-09-13 19:34:56

标签: python django passwords md5

我从一个旧的php应用程序中获得了一个用户表,其中用户将未经删除的md5哈希值作为密码,因为我正在将应用程序迁移到django,我试图将所有用户放在auth_user表中。

引用this帖子,可以将密码存储为没有盐的md5哈希值。但那对我不起作用? (python / 2.7.6,django / 1.6.1)

例如对于有密码" changeme"我认为它应该采用格式md5 $$ 4cb9c8a8048fd02294477fcb1a41191a或者我错过了什么?

在settings.py中的

编辑:我有:

PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
    'django.contrib.auth.hashers.BCryptPasswordHasher',
    'django.contrib.auth.hashers.SHA1PasswordHasher',
    'django.contrib.auth.hashers.MD5PasswordHasher',
    'django.contrib.auth.hashers.CryptPasswordHasher',
)

我在views.py中使用login_required装饰器,如果以某种方式相关:

@login_required
def index(request):
    logger.debug('index accessed from %s by %s' % (request.META.get('REMOTE_ADDR'), request.user.username) )
    member = members.objects.get(nickname=request.user.username)
    context = {'request': request, 'member': member}
    return render(request, 'voip/index.html', context)

并关注urls.py:

url(r'^login/$', 'django.contrib.auth.views.login', {
  'template_name': 'voip/login.html'
}),
url(r'^logout/$', 'django.contrib.auth.views.logout_then_login', {
  #'template_name': 'voip/logout.html'
}),

只要在settings.py AUTHENTICATION_BACKENDS看起来像这样:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'django_auth_ldap.backend.LDAPBackend',
)

一旦我评论出django_auth_ldap它不起作用。但是如果我然后将pbkdf2哈希从最初安装的超级用户(我已经设置pw changeme进行调试)复制到auth_user表中我自己的用户,我可以使用密码登录" changeme" ...

3 个答案:

答案 0 :(得分:2)

从我在Django 1.6.1源代码中看到的内容,您不能将MD5PasswordHasher与空盐一起使用:https://github.com/django/django/blob/1.6.1/django/contrib/auth/hashers.py#L397

UnsaltedMD5PasswordHasher可能对您有用。

编辑:你提到的answer写于4年前,当时Django 1.2统治了市场。我检查过它password hashing code并且它没有任何断言,这就是为什么MD5在那时使用空盐的原因。

答案 1 :(得分:1)

我对你的问题有两点建议。

首先,请检查PASSWORD_HASHERS中的settings.py。 Django能够从旧算法升级密码,但前提是它们在您的配置中可用。阅读更多at the django docs

至少你需要激活MD5PasswordHasher

PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.MD5PasswordHasher',
)

其次,如果您已经这样做了,您可以尝试简单地存储旧MD5密码而不引导md5$$。这也是一种后备支持。 Django将识别32位十六进制数字作为MD5哈希。这是django source code

中的相关代码块
# Ancient versions of Django created plain MD5 passwords and accepted
# MD5 passwords with an empty salt.
if ((len(encoded) == 32 and '$' not in encoded) or
        (len(encoded) == 37 and encoded.startswith('md5$$'))):
    algorithm = 'unsalted_md5'

希望这有帮助!

答案 2 :(得分:-1)

您可以自定义身份验证过程,甚至可以编写自定义身份验证后端。官方文档中包含此主题:

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/

相关问题