隨機生成密碼雹嗦,非常常見又通用的場景,大家可以參考一下合是,包含:數(shù)字了罪、大小寫字母、特殊符號端仰。
/**
* 密碼工具類
*
* @author haozi2015@aliyun.com
*/
public class PasswordUtils {
private static final int MIN_NUMBER = 33;
private static final int MAX_NUMBER = 126;
private static final int BOUND = MAX_NUMBER - MIN_NUMBER + 1;
/**
* 隨機生成指定位數(shù)的密碼
*
* char[33,126]捶惜,可表示數(shù)字、大小寫字母荔烧、特殊字符
*
* @param length 密碼長度
* @return
*/
public static String randomPassword(int length) {
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
char value = (char) (random.nextInt(BOUND) + MIN_NUMBER);
builder.append(value);
}
return builder.toString();
}
public static void main(String[] args) throws Exception {
for (char i = 0; i < 100; i++) {
System.out.println(randomPassword(10));
}
}
}