How can I set salt for bcrypt.hashpw?

限于喜欢 提交于 2019-12-07 16:06:59

问题


    salt = 'yhnqazolr123098765'
    password = bcrypt.hashpw(password,salt)
    repeatpassword = bcrypt.hashpw(repeatpassword,salt)

I got error for the second line.

ValueError at /register

Invalid salt

Request Method:     POST
Request URL:    http://127.0.0.1:8000/register
Django Version:     1.3.1
Exception Type:     ValueError
Exception Value:    

Invalid salt

Exception Location:     /home/user1/djangoblog/blog/views.py in register, line 70
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/user1/djangoblog',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/pymodules/python2.6/gtk-2.0']

What to do? I need to set some default salt value rather than random salt every time.


回答1:


I think that, as in the example found in the project page, you need something like this:

salt = bcrypt.gensalt()
password = bcrypt.hashpw(password, salt)
repeatpassword = bcrypt.hashpw(repeatpassword,salt)



回答2:


The format for salt is:

$Version$log2(NumRounds)$salt

where:

  • Version is 2,
  • 0 <= log2(NumRounds) < 32,
  • salt is a 22-byte base-64 encoded string.

I suggest you use bcrypt.gensalt() instead. There's no good reason for you to provide your own salt.



来源:https://stackoverflow.com/questions/8869367/how-can-i-set-salt-for-bcrypt-hashpw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!