如何为bcrypt.hashpw设置盐?

时间:2012-01-15 11:44:08

标签: python django hash bcrypt

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

第二行出错了。

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']

怎么办? 我每次都需要设置一些默认的盐值而不是随机盐。

2 个答案:

答案 0 :(得分:2)

我认为,就像在project page中找到的示例一样,你需要这样的东西:

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

答案 1 :(得分:2)

salt的格式为:

$Version$log2(NumRounds)$salt

其中:

  • Version2
  • 0 <= log2(NumRounds) < 32
  • salt是一个22字节的base-64编码字符串。

我建议您改用bcrypt.gensalt()。你没有充分的理由提供自己的盐。