Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.progressoft.jip8;
- import java.security.SecureRandom;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class PasswordGenerator {
- public String generate() {
- return generateRandomPassword(8);
- }
- static String chars = "abcdefghijklmnopqrstuvwxyz";
- static String nums = "0123456789";
- static String sChars = "_$@%";
- static String password = chars + nums + sChars;
- static String password_shuffle = shuffleString(password);
- static String ok_password = password_shuffle;
- static SecureRandom random = new SecureRandom();
- public static void main(String[] args) {
- System.out.println("password : " + generateRandomPassword(8));
- System.out.println("\n");
- }
- public static String generateRandomPassword(int length) {
- if (length < 1) throw new IllegalArgumentException();
- int count_chars = 0, count_num = 0, count_schar = 0;
- StringBuilder sb = new StringBuilder(length);
- for (int i = 0; i < length;) {
- int rndCharAt = random.nextInt(ok_password.length());
- char rndChar = ok_password.charAt(rndCharAt);
- if (chars.contains(rndChar + "") && count_chars <4){
- sb.append(rndChar);
- count_chars++;
- i++;
- }
- if (nums.contains(rndChar + "") && count_num <2){
- sb.append(rndChar);
- count_num++;
- i++;
- }
- if (sChars.contains(rndChar + "") && count_schar <2){
- sb.append(rndChar);
- count_schar++;
- i++;
- }
- }
- return sb.toString();
- }
- public static String shuffleString(String string)
- {
- List<String> letters = Arrays.asList(string.split(""));
- Collections.shuffle(letters);
- String shuffled = "";
- for (String letter : letters) {
- shuffled += letter;
- }
- return shuffled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement