Here you can find the source of generatePassword(int length, String combination)
Parameter | Description |
---|---|
length | the desired password length. |
combination | the letterset used in the generation process. |
public static String generatePassword(int length, String combination)
//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(); } }