forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reto mouredev#3 - Generador de contraseñas
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
Retos/Reto #3 - EL GENERADOR DE CONTRASEÑAS [Media]/java/vandresca.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import java.util.*; | ||
|
||
/** | ||
* Programa que genere una contraseña en la que se pueda: | ||
* - Elegir si tiene 8 o 16 carácteres | ||
* - Elegir si se quiere que tenga mayúsculas | ||
* - Elegir si se quiere que tenga números | ||
* - Elegir si se quiere que tenga simbolos | ||
*/ | ||
public class vandresca { | ||
|
||
private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; | ||
private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
private static final String NUMBERS = "0123456789"; | ||
private static final String SYMBOLS = "!@#$%^&*()_+-=[]|,./?><"; | ||
|
||
|
||
public static class Params{ | ||
public Boolean hasLength16; | ||
public Boolean hasUpperCase; | ||
public Boolean hasNumbers; | ||
public Boolean hasSymbols; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Params params = new Params(); | ||
params.hasLength16 = true; | ||
params.hasUpperCase = true; | ||
params.hasNumbers = true; | ||
params.hasSymbols = true; | ||
showPassword(generatePassword(params)); | ||
} | ||
|
||
private static String generatePassword(Params params) { | ||
String sourceChars = getSourceChars(params); | ||
int length = (params.hasLength16)? 16 : 8; | ||
String password = ""; | ||
for(int i=0; i<length; i++){ | ||
password += randomChar(sourceChars); | ||
} | ||
return password; | ||
} | ||
private static String getSourceChars(Params params){ | ||
|
||
|
||
StringBuilder sourceChars = new StringBuilder(LOWERCASE); | ||
if(params.hasUpperCase){ | ||
sourceChars.append(UPPERCASE); | ||
} | ||
if(params.hasNumbers){ | ||
sourceChars.append(NUMBERS); | ||
} | ||
if(params.hasSymbols){ | ||
sourceChars.append(SYMBOLS); | ||
} | ||
List<String> source = Arrays.asList(sourceChars.toString()); | ||
Collections.shuffle(source); | ||
return source.get(0); | ||
} | ||
|
||
private static String randomChar(String sourceChars){ | ||
Random rd = new Random(); | ||
int randomInt = rd.nextInt(sourceChars.length()); | ||
return String.valueOf(sourceChars.charAt(randomInt)); | ||
} | ||
|
||
private static void showPassword(String password){ | ||
System.out.println(password); | ||
} | ||
} | ||
|