如何查找存储在django.contrib.auth.models.User中的密码

时间:2011-11-11 21:01:55

标签: django

我想查看我为DB命名为'user'的用户存储在DB中的密码。

这就是我所做的。

user@ubuntu:~/Documents/Django/django_bookmarks$ python manage.py shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(id=1)
>>> user.username, user.password
(u'user', u'sha1$6934a$f92c73726c0bd5d4821013ad4161578a2114090f')
>>> 
>>> import hashlib
>>> hexhash = hashlib.sha1("password")
>>> hexhash
<sha1 HASH object @ 0x99c18c0>
>>> hexhash.digest
<built-in method digest of _hashlib.HASH object at 0x99c18c0>

我记得我使用'密码'作为用户密码,但我无法验证。

问题&GT;如何找出用户的密码?

谢谢

3 个答案:

答案 0 :(得分:6)

您可以使用check_passwordhttps://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.models.User.check_password

查看用户的密码
from django.contrib.auth.models import User

user = User.objects.get(id=1)
user.check_password('password') # Returns True or False

答案 1 :(得分:2)

django已经对你的passwd进行了哈希处理,这是一个仅在某种程度上有效的功能。

您可以尝试在hash database上搜索sha1,但他们无法保证找到它。

你应该搜索'f92c73726c0bd5d4821013ad4161578a2114090f'。散列函数是sha1,用于散列的键是'6934a'

答案 2 :(得分:1)

您无法获得已设置的实际密码。 set_password方法将原始密码转换为sha1代码。 您只能检查密码,这是否正确。

也请查看此链接

https://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.models.User.check_password