擅长:python、mysql、java
<p>使用bcrypt生成一个salt并保存在设置文件中:</p>
<pre><code>import bcrypt
salt = bcrypt.gensalt()
</code></pre>
<p><strong>加密密码:</strong></p>
<pre><code>password = "userpassword"
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
</code></pre>
<p><strong>检查生成的盐:</strong></p>
<pre><code>>>> print hashed
$2a$12$C.zbaAxJPVVPKuS.ZvNQiOTVSdOf18kMP4qDKDnM3AGrNyGO5/tTy
</code></pre>
<p>要检查给定的密码是否与生成的密码匹配(只需使用salt创建密码的哈希并将其与数据库中的密码进行比较):</p>
<pre><code>given_password = "password"
hashed_password = bcrypt.hashpw(password, salt) #Using the same salt used to hash passwords on your settings
hashed_password == hashed #In this case it returns false, because passwords are not the same
</code></pre>