Java Password Generate generatePassword(int length)

Here you can find the source of generatePassword(int length)

Description

generate Password

License

Apache License

Declaration

public static String generatePassword(int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    private static final String DIGITS = "0123456789";
    private static final String LETTERS = "abcdefghijklmnopqrstuvwxyz";
    private static final String SPECIALS = "!@#$%^&*()_+-=,.?/;:{}[]|`~<>'";

    public static String generatePassword(int length) {
        if (length <= 0) {
            return "";
        }/*  ww w .  ja va  2 s  . c  o m*/
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        String exCludeChar = "'<>&";
        String[] charSets = { DIGITS, LETTERS, SPECIALS };
        for (int i = 0; i < length; i++) {
            int r = random.nextInt(charSets.length);
            String charSet = charSets[r];
            r = random.nextInt(charSet.length());
            char c = charSet.charAt(r);
            if (exCludeChar.contains(String.valueOf(c))) {
                // exclude
                i--;
                continue;
            }
            sb.append(charSet.charAt(r));
        }
        return sb.toString();
    }
}

Related

  1. generatePassword()
  2. generatePassword()
  3. generatePassword()
  4. generatePassword(int lenght)
  5. generatePassword(int length)
  6. generatePassword(int length, boolean special)
  7. generatePassword(int length, String combination)
  8. generatePassword(int minLen, int maxLen, int noOfCAPSAlpha, int noOfDigits, int noOfSplChars)
  9. generateString(final String letters, int length)