Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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();
	}
}
Comment

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 int array 
Java :: how to get the last element of array in java 
Java :: transformez integer en string 
Java :: How to efficiently find the highest power of 2 dividing a given number, in Java? 
Java :: print line in jjava 
Java :: get boolean from string java 
Java :: java ModelMapper class 
Java :: simple java program 
Java :: how to reset jframe java swing 
Java :: get file dimension java 
Java :: java 8 group a collections by 2 property 
Java :: array string remove element java 
Java :: java swing alert window 
Java :: what is import java.io.*? 
Java :: java hello world 
Java :: how to set title in center in toolbar in android 
Java :: create method setsupportactionbar 
Java :: instantiate matrix java 
Java :: android get text from string xml programmatically 
Java :: how to wait in java 
Java :: Java Hashmap Access Elements 
Java :: how to reverse a string in java 
Java :: read integer input java 
Java :: spring boot procfile run profile 
Java :: java foreach 
Java :: android date 
Java :: android sharedpreferences 
Java :: n queens problem leetcode 
Java :: java indexof array 
Java :: javafx resizable window 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =