0

我有这样的看法

def Registration(request):
    RegForm = RegistrationForm(request.POST or None)
    if request.method == 'POST':
        if RegForm.is_valid():
            clearUserName = RegForm.cleaned_data['userNm']   
            clearPass = RegForm.cleaned_data['userPass']

            hashedpasswithsalt = bcrypt.hashpw(clearPass, bcrypt.gensalt(14))

            RegForm.save()
            try:
                return HttpResponseRedirect('/Newuser/?userNm=' + clearUserName)
            except:
                raise ValidationError(('Invalid request'), code='300')    ## [ TODO ]: add a custom error page here.
    else:
        RegForm = RegistrationForm()

        return render(request, 'VA/reuse/register.html', {
            'RegForm': RegForm 
        })

报名表格

class RegistrationForm(ModelForm):
    userPass = forms.CharField(widget=forms.PasswordInput, label='Password')
    class Meta:
        model = Client
        fields = ['userNm','userPass']

为什么它以明文形式存储?

我正在尝试从模型中获取cleaned_data[]ofuserPass并在发送到数据库之前对其进行哈希处理。

4

1 回答 1

0

试试bcrypt.hashpw(clearPass.encode("utf-8"), bcrypt.gensalt(14))

这是因为您的 clearPass 默认情况下是一个 Unicode 对象,并且不能直接在您的散列函数中使用,encode("utf-8")将其转换为标准字符串,然后可以对其进行散列。

于 2013-08-21T02:34:51.530 回答