package com.xxx.xxx.common.utils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Random;
/**
* 密码工具类
*/
public class PwdUtil {
public static String encode(String password) {
PasswordEncoder encoder = encoder();
return encoder.encode(password);
}
public static PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
public static void main(String[] args) {
String pwd1 = encode("123456");
System.out.println(pwd1);
String pwd2 = encode("123456");
System.out.println(pwd2);
System.out.println(encoder().matches("123456", pwd1));
System.out.println(encoder().matches("123456", pwd2));
//随机生成8位密码
System.out.println(getPassword());
}
//此处转载:https://blog.csdn.net/graceup/article/details/40398655 随机生成8位密码 工具类
public static String getPassword() {
Random rd = new Random(); // 创建随机对象
String n = ""; // 保存随机数
int rdGet; // 取得随机数
do {
if (rd.nextInt() % 2 == 1) {
rdGet = Math.abs(rd.nextInt()) % 10 + 48; // 产生48到57的随机数(0-9的键位值)
} else {
rdGet = Math.abs(rd.nextInt()) % 26 + 97; // 产生97到122的随机数(a-z的键位值)
}
char num1 = (char) rdGet; // int转换char
String dd = Character.toString(num1);
n += dd;
} while (n.length() < 8);// 设定长度,此处假定长度小于8
return n;
}
}
密码工具类
最新推荐文章于 2022-07-10 12:10:30 发布