Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django将密码保存到数据库表之前的最佳哈希方法_Python_Database_Django_Hash_Passwords - Fatal编程技术网

Python django将密码保存到数据库表之前的最佳哈希方法

Python django将密码保存到数据库表之前的最佳哈希方法,python,database,django,hash,passwords,Python,Database,Django,Hash,Passwords,在保存到数据库表之前,我需要对userf.data['password']和userf.data['repeatpassword']应用哈希方法 哪种散列方法更适合使用python进行散列?使用 下面是一个来自以下方面的示例: 您可以找到有关如何为django.contrib.auth执行此操作的说明。有关详细信息,您还可以查看中的make_password功能。您可以发布代码如何使用该bcrypt吗?那会更有帮助。 def register(request): flag = True

在保存到数据库表之前,我需要对
userf.data['password']
userf.data['repeatpassword']
应用哈希方法

哪种散列方法更适合使用python进行散列?

使用

下面是一个来自以下方面的示例:


您可以找到有关如何为
django.contrib.auth
执行此操作的说明。有关详细信息,您还可以查看中的
make_password
功能。

您可以发布代码如何使用该bcrypt吗?那会更有帮助。
def register(request):
    flag = True
    possible = '0123456789abcdefghijklmnopqrstuvwxyz'
    token = ''

    current_datetime = datetime.datetime.now()

    user = UsersModelForm()
    if request.method == 'POST':
        userf = UsersModelForm(request.POST)
        username = userf.data['username']
        password = userf.data['password']
        passwordrepeat = userf.data['passwordrepeat']
        email = userf.data['email']

        if password != passwordrepeat:
            flag = False
            passVariable = {'user':user, 'flag': False}
            return render_to_response('register.html', passVariable, context_instance=RequestContext(request))

        elif password == passwordrepeat:
            for i in range(1,10):
                temp = random.choice(possible)
                token = token + temp

            print token
            if userf.is_valid():
                check = userf.save(commit=False)
                check.email_token = token
                check.email_token_expiry = current_datetime + timedelta(1)
                check.save()
                return HttpResponseRedirect('/')
    else:
        return render_to_response('register.html', {"user": user, 'flag': True}, context_instance=RequestContext(request))
import bcrypt

# Hash a password for the first time
hashed = bcrypt.hashpw(password, bcrypt.gensalt())

# gensalt's log_rounds parameter determines the complexity
# the work factor is 2**log_rounds, and the default is 12
hashed = bcrypt.hashpw(password, bcrypt.gensalt(10))

# Check that an unencrypted password matches one that has
# previously been hashed
if bcrypt.hashpw(plaintext, hashed) == hashed:
    print "It matches"
else:
    print "It does not match"