Java Password Generate generatePassword(int length, String combination)

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

Description

Generates a random password of length equal to length , consisting only of the characters contained in combination .

License

Open Source License

Parameter

Parameter Description
length the desired password length.
combination the letterset used in the generation process.

Return

the generated password.

Declaration

public static String generatePassword(int length, String combination) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .j  ava2 s .  c  om
 * SecurityHelper.java
 *
 * Copyright (C) 2012-2014 LucasEasedUp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Random;

public class Main {
    /**
     * Generates a random password of length equal to {@code length},
     * consisting only of the characters contained in {@code combination}.
     * 
     * <p> If {@code combination} contains more than one occurrence of a character,
     * the overall probability of using it in password generation will be higher.
     * 
     * @param length      the desired password length.
     * @param combination the letterset used in the generation process.
     * 
     * @return the generated password.
     */
    public static String generatePassword(int length, String combination) {
        char[] charArray = combination.toCharArray();
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();

        for (int i = 0, n = charArray.length; i < length; i++) {
            sb.append(charArray[random.nextInt(n)]);
        }

        return sb.toString();
    }
}

Related

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