JBCrypt对管理密码非常方便,采用hash+salt的方式,使用加盐的方式来对密码进行加密,并将salt加入到hash里面去,从而使得salt不需要单独保存
相比于其他的加密方案来说,更加快捷方便
依赖
<!-- pom.xml -->
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.3m</version>
</dependency>
加密
加盐 BCrypt.gensalt()
生成 salt 的方法,一般不独立使用,作为 hash 加密的一部分进行使用
加密 BCrypt.hashpw("password", BCrypt.gensalt(4))
生成 hash 密码,传入用户密码和 salt, slat 由 BCrypt.gensalt() 生成,以 int 作为参数只是为了给 salt 加上前缀,方便计算 hash 的时候使用
匹配 BCrypt.checkpw("password", hashed)
密码验证,传入密码候选值和 hash 加密过的密码,返回布尔值
原文:https://www.cnblogs.com/greyhuhu/p/12979455.html