public static String generateRandomPassword ( final CharacterTypes characterTypes , final int length ) { final StringBuffer buffer = new StringBuffer ( ) ; String characters = "" ; switch ( characterTypes ) { case ALPHA : characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ; break ; case ALPHANUMERIC : characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ; break ; case ALPHANUMERICSYMBOL : characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()" ; break ; case NUMERIC : characters = "1234567890" ; break ; } final int charactersLength = characters . length ( ) ; final SecureRandom rnd = new SecureRandom ( ) ; for ( int i = 0 ; i < length ; i ++ ) { final double index = rnd . nextInt ( charactersLength ) ; buffer . append ( characters . charAt ( ( int ) index ) ) ; } return buffer . toString ( ) ; }
Generates a random password of the specified length with the specified characters .