常用的密码加密,spring框架
以BCryptPasswordEncoder加密为例:
import org.springframework.security.crypto.password.PasswordEncoder;
public static void main(String[] args) {
String pass = "admin";
PasswordEncoder encoder = new BCryptPasswordEncoder();
final String passHash = encoder.encode(pass);
System.out.println(passHash);
final boolean matches = encoder.matches(pass, passHash);
System.out.println(matches);
}
PasswordEncoder 一般提供了encode(加密)及matches(验证)
场景:开启了密码加密存储,那么之前的明文需要转换成加密,pg数据库也提供了相应的插件:
CREATE EXTENSION pgcrypto
update t_user set passwd = crypt(passwd ,gen_salt('bf',10));
gen_salt 的算法比较:
nodejs 也提供相应的算法:
const bcrypt = require('bcryptjs')
// 随机盐
const salt = bcrypt.genSaltSync(10)
// 对明文加密
const pwd = bcrypt.hashSync('123', salt)
// 验证,true表示密码一致,false表示不一致
const isMatch = bcrypt.compareSync('123', pwd)
console.log(pwd,isMatch)
这里需要安装依赖 npm i bcryptjs
备注:以上加密后匹配结果是一样,目前只探索了bcrypt,更多算法待探究