Do you think you can do it?
Let's see what you got. 😁
package com.codegym.task.task32.task3204;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;
/*
Password generator
*/
public class Solution {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream password = getPassword();
System.out.println(password.toString());
}
/**
* Password generator. 8 char long password
* need 3 ranges of ASCII for 0-9, a-z and A-Z
* at least 1 nr, 1 lowercase letter, 1 uppercase letter
* 0-9 - 48-57
* a-z - 97-122
* A-Z - 65-90
* @return
* ByteArrayOutputStream
*/
/*
generate chars until 1 number
then generate chars until 1 lower case
then generate chars until 1 uppercase
then generate random 5 chars
randomize the position of the 5 chars inside the list to generate the final pass
*/
public static ByteArrayOutputStream getPassword() throws IOException {
ArrayList<Integer> password = new ArrayList<>();
//for 0-9
int randomNum = ThreadLocalRandom.current().nextInt(48, 57+1);
//for a-z
int randomLwrCs = ThreadLocalRandom.current().nextInt(97, 122 + 1);
//for A-Z
int randomUprCs = ThreadLocalRandom.current().nextInt(65, 90 + 1);
password.add(randomNum);
password.add(randomLwrCs);
password.add(randomUprCs);
while (password.size() < 8){
int random = ThreadLocalRandom.current().nextInt(48, 123);
if ((random >= 48 && random < 58) || (random >= 97 && random < 123)
|| (random >= 65 && random < 91)){
password.add(random);
}
}
Collections.shuffle(password);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
for (int element : password) {
out.write(element);
}
return baos;
}
}