[英]No hashing while trying to Hash using Python BCrypt
I have a view like: 我有这样的看法 :
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
})
RegistrationForm 报名表格
class RegistrationForm(ModelForm):
userPass = forms.CharField(widget=forms.PasswordInput, label='Password')
class Meta:
model = Client
fields = ['userNm','userPass']
Why is it storing in plaintext? 为什么以明文形式存储?
I'm trying to take the cleaned_data[]
of userPass
from a modelfrom and hash it prior to sending to the db. 我正在尝试从modelfrom中获取userPass
的cleaned_data[]
并对其进行哈希处理,然后再发送到db。
Try bcrypt.hashpw(clearPass.encode("utf-8"), bcrypt.gensalt(14))
. 尝试bcrypt.hashpw(clearPass.encode("utf-8"), bcrypt.gensalt(14))
。
This is because your clearPass is by default an Unicode object and that it can't be directly used in your hashing function, the encode("utf-8")
converts it into a standard string and then it can be hashed. 这是因为默认情况下,您的clearPass是Unicode对象,并且不能直接在哈希函数中使用,它的encode("utf-8")
将其转换为标准字符串,然后可以对其进行哈希处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.