尝试使用Python BCrypt进行哈希时不进行哈希
我有一个观点像:尝试使用Python BCrypt进行哈希时不进行哈希,python,django,hash,bcrypt,Python,Django,Hash,Bcrypt,我有一个观点像: def Registration(request): RegForm = RegistrationForm(request.POST or None) if request.method == 'POST': if RegForm.is_valid(): clearUserName = RegForm.cleaned_data['userNm'] clearPass = RegForm.cle
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']
为什么它存储在纯文本中
我正试图从modelfrom中获取userPass
的cleaned_data[]
,并在发送到db之前对其进行散列。尝试bcrypt.hashpw(clearPass.encode(“utf-8”),bcrypt.gensalt(14))
这是因为您的clearPass在默认情况下是一个Unicode对象,并且不能直接在哈希函数中使用,
encode(“utf-8”)
将其转换为标准字符串,然后可以对其进行哈希处理。Try:from django.utils import force\u Unicode clearPass=force\u Unicode(RegForm.cleaned\u data['userPass'))这似乎有效,但密码仍然以明文形式存储。有什么原因吗?是的,我确信它们仍然以明文形式存储。我认为这是因为hashedpasswithsalt没有分配给RegForm.cleaned_data['userPass']”不幸的是,它仍然以明文形式存储。