2017-10-22 21:11:27 +08:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates a random password from ASCII letters
|
2017-10-25 15:13:41 +08:00
|
|
|
Given password length bounds
|
2017-10-22 21:11:27 +08:00
|
|
|
|
|
|
|
author: AKS1996
|
2017-10-25 15:13:41 +08:00
|
|
|
date: 2017-10-25
|
2017-10-22 21:11:27 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
class PasswordGen {
|
|
|
|
public static void main(String args[]){
|
2017-10-25 15:13:41 +08:00
|
|
|
String password = generatePassword(8,16);
|
|
|
|
System.out.print("Password: " + password);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String generatePassword(int min_length, int max_length){
|
2017-10-22 21:11:27 +08:00
|
|
|
Random random = new Random();
|
|
|
|
|
|
|
|
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
String lower = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
String numbers = "0123456789";
|
|
|
|
String specialChars = "!@#$%^&*(){}?";
|
|
|
|
|
|
|
|
String allChars = upper+lower+numbers+specialChars;
|
|
|
|
|
|
|
|
List<Character> letters = new ArrayList<Character>();
|
|
|
|
for(char c:allChars.toCharArray())
|
|
|
|
letters.add(c);
|
|
|
|
|
|
|
|
// Inbuilt method to randomly shuffle a elements of a list
|
|
|
|
Collections.shuffle(letters);
|
|
|
|
String password = "";
|
|
|
|
|
|
|
|
// Note that size of the password is also random
|
|
|
|
for(int i = random.nextInt(max_length-min_length) + min_length; i>0; --i) {
|
|
|
|
password += letters.get(random.nextInt(letters.size()));
|
|
|
|
}
|
|
|
|
|
2017-10-25 15:13:41 +08:00
|
|
|
return password;
|
2017-10-22 21:11:27 +08:00
|
|
|
}
|
|
|
|
}
|