package cn.itcast.encode; import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils; import java.io.UnsupportedEncodingException; public class MD5Demo {
public static void main(String[] args) throws UnsupportedEncodingException {
// String password = "liu123";
// String password = "mozq123";
String password = "liubei123";
// String s = DigestUtils.md5DigestAsHex(password.getBytes()); //进行加盐
// String salt = "mozq"; //生成随机盐值
// String salt = RandomStringUtils.random(10);
// String salt = RandomStringUtils.randomAlphabetic(10);
String salt = RandomStringUtils.randomAlphabetic(5, 8);
System.out.println(salt);
System.out.println(salt.length()); String passwordSalt = salt + password;
//获取MD5加密工具,进行加密
// String s = DigestUtils.md5DigestAsHex(passwordSalt.getBytes()); /*
Spring给出的加密解决方案
*/
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String s = encoder.encode(password);
//$2a$10$RcVrPsuSd3BPiBWdXF.ncO96Ls99VSIY92zZ0.z7F6jFPmjsRzaOC
//$2a$10$M.fk6zjeH/7By7Krcem0u.wWsdSiorJ2do98QLZJjssUWwx2eCfeW
System.out.println(s);
System.out.println(s.length());
}
}
Spring加密工具BCryptPasswordEncoder