Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

random password generator java

public class PasswordGenerator {
	public static String generateRandomPassword(int len) {
		String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi"
          +"jklmnopqrstuvwxyz!@#$%&";
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++)
			sb.append(chars.charAt(rnd.nextInt(chars.length())));
		return sb.toString();
	}
}
Comment

java generate secure random password

public class PasswordUtils {

    static char[] SYMBOLS = "^$*.[]{}()?-"!@#%&/,><':;|_~`".toCharArray();
    static char[] LOWERCASE = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    static char[] UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    static char[] NUMBERS = "0123456789".toCharArray();
    static char[] ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789^$*.[]{}()?-"!@#%&/,><':;|_~`".toCharArray();
    static Random rand = new SecureRandom();

    public static String getPassword(int length) {
        assert length >= 4;
        char[] password = new char[length];

        //get the requirements out of the way
        password[0] = LOWERCASE[rand.nextInt(LOWERCASE.length)];
        password[1] = UPPERCASE[rand.nextInt(UPPERCASE.length)];
        password[2] = NUMBERS[rand.nextInt(NUMBERS.length)];
        password[3] = SYMBOLS[rand.nextInt(SYMBOLS.length)];

        //populate rest of the password with random chars
        for (int i = 4; i < length; i++) {
            password[i] = ALL_CHARS[rand.nextInt(ALL_CHARS.length)];
        }

        //shuffle it up
        for (int i = 0; i < password.length; i++) {
            int randomPosition = rand.nextInt(password.length);
            char temp = password[i];
            password[i] = password[randomPosition];
            password[randomPosition] = temp;
        }

        return new String(password);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(getPassword(8));
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java int to biginteger 
Java :: java define empty string list 
Java :: select word from list 
Java :: setbounds in java 
Java :: java 11 download 64 bit 
Java :: java find longest string in list 
Java :: sum of 1D array(JAVA) 
Java :: data java 
Java :: java long to int 
Java :: @data lombok 
Java :: java leap year expression 
Java :: java get sub array 
Java :: java date string 
Java :: java scanner 
Java :: set jframe fullscreen 
Java :: java selenium send keys numbers 
Java :: java arraylist to string 
Java :: byte array to base64 string 
Java :: convert string to character array 
Java :: java variables in annotations 
Java :: math.sin java 
Java :: remove fragment from backstack 
Java :: java flowlayout 
Java :: Why should we mention a method throws some exception/s? 
Java :: start activity for result deprecated 
Java :: mobile number validation to edittext in android 
Java :: java camel case 
Java :: string to byte array java utf-8 
Java :: how to print an array in java 
Java :: convert arraylist to array in java 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =