python – 将密码保存到django的数据库表之前的最佳散列方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 将密码保存到django的数据库表之前的最佳散列方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1587字,纯文字阅读大概需要3分钟。
内容图文
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))
在保存到数据库表之前,我需要对userf.data [‘password’]和userf.data [‘repeatpassword’]应用散列方法.
哪种散列方法更适合使用python进行散列?
解决方法:
使用bcrypt.
以下是README的一个例子:
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"
内容总结
以上是互联网集市为您收集整理的python – 将密码保存到django的数据库表之前的最佳散列方法全部内容,希望文章能够帮你解决python – 将密码保存到django的数据库表之前的最佳散列方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。