📄 passwordcreator.java
字号:
package org.tuna.util;
public class PasswordCreator
{
public static final int DIGIT = 1; /*数字*/
public static final int ALPHA = 2; /*字母*/
public static final int PUNCT = 4; /*标点*/
public static final int OTHER = 8; /*自定*/
private int charType;
private int pwdLen;
private char[] digits = "1234567890".toCharArray();
private char[] alphas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private char[] puncts = "`~!@#$%^&*()-=\\[];',./_+|{}:\"<>?".toCharArray();
private String others = "";
private char[] pwdArray;
/**
* 默认格式,数字加字母,长度8
*/
public PasswordCreator()
{
charType = 3;
pwdLen = 8;
others = "";
creatChars();
}
/**
* 以默认格式指定长度构造
*/
public PasswordCreator(int len)
{
charType = 3;
pwdLen = len;
others = "";
creatChars();
}
/**
* 这里没有指定符号,所以当类型大于7时,自动设为3
*/
public PasswordCreator(int type, int len)
{
charType = type;
if (charType >= 8) charType = 3;
pwdLen = len;
others = "";
creatChars();
}
/**
* 自定义字符与标准字符的混合(似乎有些多余)
* @param type 类型
* @param oth 自定的字符组成的字符串
* @param len 长度
*/
public PasswordCreator(int type, String oth, int len)
{
charType = type;
others = oth;
pwdLen = len;
creatChars();
}
/**
* 字符集为自定义字符
* @param cs 自定的字符组成的字符串
* @param len 长度
*/
public PasswordCreator(String cs, int len)
{
charType = 8;
others = cs;
pwdLen = len;
creatChars();
}
// ------ Public Methods ------
public String getPassword()
{
int len = pwdArray.length;
char[] temp = new char[pwdLen];
try{
for(int i = 0; i < pwdLen; i++)
temp[i] = pwdArray[(int)(Math.random() * len)];
}
catch(Exception e){System.out.println("length:" + pwdLen + "\n" + len);}
return new String(temp);
}
public void setLength(int len)
{
pwdLen = len;
}
public void setOtherChar(String cs)
{
others = cs;
creatChars();
}
public void setType(int type)
{
charType = type;
if (charType == 8 && others.length() == 0)
others = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
creatChars();
}
// ------ Private Methods ------
private void creatChars()
{
String dgt = new String(digits);
String alp = new String(alphas);
String pnt = new String(puncts);
String oth = others;
switch(charType){
case 1 : pwdArray = dgt.toCharArray(); break;
case 2 : pwdArray = alp.toCharArray(); break;
case 3 : pwdArray = (dgt + alp).toCharArray(); break;
case 4 : pwdArray = pnt.toCharArray(); break;
case 5 : pwdArray = (dgt + pnt).toCharArray(); break;
case 6 : pwdArray = (alp + pnt).toCharArray(); break;
case 7 : pwdArray = (dgt + alp + pnt).toCharArray(); break;
case 8 : pwdArray = oth.toCharArray(); break;
case 9 : pwdArray = (dgt + oth).toCharArray(); break;
case 10: pwdArray = (alp + oth).toCharArray(); break;
case 11: pwdArray = (dgt + alp + oth).toCharArray(); break;
case 12: pwdArray = pnt.toCharArray(); break;
case 13: pwdArray = (dgt + pnt + oth).toCharArray(); break;
case 14: pwdArray = (alp + pnt + oth).toCharArray(); break;
case 15: pwdArray = (dgt + alp + pnt + oth).toCharArray(); break;
default: return;
}
}
// ------ For Test ------
public static void main(String[] args)
{
PasswordCreator pc = new PasswordCreator(4, 10);
for(int i = 0; i < 8; i++){
pc.setType(i + 1);
pc.setLength(i + 10);
pc.setOtherChar("abcde");
System.out.println(pc.getPassword());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -