Answers for "java generate random password"

1

generate random password in java

import java.util.Random;

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();
	}
}
Posted by: Guest on July-16-2020
0

generta epassword java

public static String generateRandomPassword(int length) {
        Object[][] characterSets = {
                {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'},
                {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'},
                {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
        };
        List<Character> passwordCharacters = new ArrayList<>();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for (Object[] characters : characterSets){
            for (int i = 0; i < length / 3; i++){
                passwordCharacters.add((Character) characters[random.nextInt(0, characters.length)]);
            }
        }
        for (int i =0; i < length % 3; i++){
            Object[] characters = characterSets[random.nextInt(0, characterSets.length)];
            passwordCharacters.add((Character) characters[random.nextInt(0, characters.length)]);
        }
        Collections.shuffle(passwordCharacters);
        StringBuilder stringBuilder = new StringBuilder(length);
        passwordCharacters.forEach(stringBuilder::append);
        return stringBuilder.toString();
    }
Posted by: Guest on November-07-2020
0

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));
        }
    }
}
Posted by: Guest on June-25-2021

Code answers related to "java generate random password"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language