Please choose below values are including pattern based on the your requirement:
Example:
// TODO: Auto-generated Javadoc
/**
* The Class RandomStringGenerator.
*/
public class RandomStringGenerator {
/** The random string length. */
private static int randomStringLength = 8;
/** The allow special characters. */
private static boolean allowSpecialCharacters = true;
/** The special characters. */
private static String specialCharacters = "!@$%*-_+:";
/** The allow duplicates. */
private static boolean allowDuplicates = false;
/** The is alphanum. */
private static boolean isAlphanum = true;
/** The is numeric. */
private static boolean isNumeric = false;
/** The is alpha. */
private static boolean isAlpha = false;
/** The Constant alphabet. */
private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
/** The mix case. */
private static boolean mixCase = true;
/** The Constant capAlpha. */
private static final String capAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/** The Constant num. */
private static final String num = "0123456789";
/**
* Gets the random string.
*
* @return the random string
*/
public static String getRandomString() {
String returnVal = "";
int specialCharactersCount = 0;
int maxspecialCharacters = randomStringLength / 4;
try {
StringBuffer values = buildList();
for (int inx = 0; inx < randomStringLength; inx++) {
int selChar = (int) (Math.random() * (values.length() - 1));
if (allowSpecialCharacters) {
if (specialCharacters.indexOf("" + values.charAt(selChar)) > -1) {
specialCharactersCount++;
if (specialCharactersCount > maxspecialCharacters) {
while (specialCharacters.indexOf("" + values.charAt(selChar)) != -1) {
selChar = (int) (Math.random() * (values.length() - 1));
}
}
}
}
returnVal += values.charAt(selChar);
if (!allowDuplicates) {
values.deleteCharAt(selChar);
}
}
} catch (Exception e) {
returnVal = "Error While Processing Values";
}
return returnVal;
}
/**
* Builds the list.
*
* @return the string buffer
*/
private static StringBuffer buildList() {
StringBuffer list = new StringBuffer(0);
if (isNumeric || isAlphanum) {
list.append(num);
}
if (isAlpha || isAlphanum) {
list.append(alphabet);
if (mixCase) {
list.append(capAlpha);
}
}
if (allowSpecialCharacters) {
list.append(specialCharacters);
}
int currLen = list.length();
String returnVal = "";
for (int inx = 0; inx < currLen; inx++) {
int selChar = (int) (Math.random() * (list.length() - 1));
returnVal += list.charAt(selChar);
list.deleteCharAt(selChar);
}
list = new StringBuffer(returnVal);
return list;
}
public static void main(String[] args) {
//do you want to password only allow numbers, please isNumeric should be true and all other attributes false
//do you want to password only allow alpha numbers, please isAlphanum should be true and all other attributes false
//do you want to password only allow alpha letters, please isAlpha should be true and all other attributes false
//do you want to password only allow special characters, please "allowSpecialCharacters" should be true
System.out.println("Ramdom Password: " + getRandomString());
}
}
- Do you want to password only allow numbers, please isNumeric should be true and all other attributes false
- Do you want to password only allow alpha numbers, please isAlphanum should be true and all other attributes false
- Do you want to password only allow alpha letters, please isAlpha should be true and all other attributes false
- Do you want to password only allow special characters, please "allowSpecialCharacters" should be true
- Do you want to create specific length password, please "randomStringLength" value should be change.
Example:
// TODO: Auto-generated Javadoc
/**
* The Class RandomStringGenerator.
*/
public class RandomStringGenerator {
/** The random string length. */
private static int randomStringLength = 8;
/** The allow special characters. */
private static boolean allowSpecialCharacters = true;
/** The special characters. */
private static String specialCharacters = "!@$%*-_+:";
/** The allow duplicates. */
private static boolean allowDuplicates = false;
/** The is alphanum. */
private static boolean isAlphanum = true;
/** The is numeric. */
private static boolean isNumeric = false;
/** The is alpha. */
private static boolean isAlpha = false;
/** The Constant alphabet. */
private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
/** The mix case. */
private static boolean mixCase = true;
/** The Constant capAlpha. */
private static final String capAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/** The Constant num. */
private static final String num = "0123456789";
/**
* Gets the random string.
*
* @return the random string
*/
public static String getRandomString() {
String returnVal = "";
int specialCharactersCount = 0;
int maxspecialCharacters = randomStringLength / 4;
try {
StringBuffer values = buildList();
for (int inx = 0; inx < randomStringLength; inx++) {
int selChar = (int) (Math.random() * (values.length() - 1));
if (allowSpecialCharacters) {
if (specialCharacters.indexOf("" + values.charAt(selChar)) > -1) {
specialCharactersCount++;
if (specialCharactersCount > maxspecialCharacters) {
while (specialCharacters.indexOf("" + values.charAt(selChar)) != -1) {
selChar = (int) (Math.random() * (values.length() - 1));
}
}
}
}
returnVal += values.charAt(selChar);
if (!allowDuplicates) {
values.deleteCharAt(selChar);
}
}
} catch (Exception e) {
returnVal = "Error While Processing Values";
}
return returnVal;
}
/**
* Builds the list.
*
* @return the string buffer
*/
private static StringBuffer buildList() {
StringBuffer list = new StringBuffer(0);
if (isNumeric || isAlphanum) {
list.append(num);
}
if (isAlpha || isAlphanum) {
list.append(alphabet);
if (mixCase) {
list.append(capAlpha);
}
}
if (allowSpecialCharacters) {
list.append(specialCharacters);
}
int currLen = list.length();
String returnVal = "";
for (int inx = 0; inx < currLen; inx++) {
int selChar = (int) (Math.random() * (list.length() - 1));
returnVal += list.charAt(selChar);
list.deleteCharAt(selChar);
}
list = new StringBuffer(returnVal);
return list;
}
public static void main(String[] args) {
//do you want to password only allow numbers, please isNumeric should be true and all other attributes false
//do you want to password only allow alpha numbers, please isAlphanum should be true and all other attributes false
//do you want to password only allow alpha letters, please isAlpha should be true and all other attributes false
//do you want to password only allow special characters, please "allowSpecialCharacters" should be true
System.out.println("Ramdom Password: " + getRandomString());
}
}
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.