From 8041233880939b36cd5ad9106b5685813d18f166 Mon Sep 17 00:00:00 2001 From: Pedro Cisneros Santana Date: Mon, 5 Oct 2020 14:02:19 +0200 Subject: [PATCH] Added PasswordGenerator in java Small exercise made for school --- PasswordGenerator_java/Password.java | 52 ++++++++++++++++++++++ PasswordGenerator_java/pruebaPassword.java | 23 ++++++++++ 2 files changed, 75 insertions(+) create mode 100644 PasswordGenerator_java/Password.java create mode 100644 PasswordGenerator_java/pruebaPassword.java diff --git a/PasswordGenerator_java/Password.java b/PasswordGenerator_java/Password.java new file mode 100644 index 000000000..3b06c9b30 --- /dev/null +++ b/PasswordGenerator_java/Password.java @@ -0,0 +1,52 @@ +package exercises; + +import java.util.Random; + +public class Password { + private int longitud = 8; + private String pass; + + public Password(String pass, int longitud) { + this.longitud = longitud; + this.pass = pass; + } + public Password(int longitud) { + this.longitud = longitud; + } + + public String getPass() { + return pass; + } + public void setPass(String pass) { + this.pass = pass; + } + public void setLongitud(int longitud) { + this.longitud = longitud; + } + + public boolean esFuerte(String pass) { + int upper = 0; + int lower = 0; + int number = 0; + for (int i = 0; i < pass.length(); i++) { + if (Character.isDigit(pass.charAt(i))) number++; + if (Character.isUpperCase(pass.charAt(i))) upper++; + if (Character.isLowerCase(pass.charAt(i))) lower++; + } + if (upper >= 2 && lower >= 1 && number >= 2) { + return true; + } else { + return false; + } + } + + public static String generarPassword(int longitud) { + String caracteres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?"; + Random rand = new Random(); + char[] pwd = new char[longitud]; + for (int i = 0; i < longitud; i++) { + pwd[i] = caracteres.charAt(rand.nextInt(caracteres.length())); + } + return String.valueOf(pwd); + } +} diff --git a/PasswordGenerator_java/pruebaPassword.java b/PasswordGenerator_java/pruebaPassword.java new file mode 100644 index 000000000..d6c89c858 --- /dev/null +++ b/PasswordGenerator_java/pruebaPassword.java @@ -0,0 +1,23 @@ +package exercises; + +import java.util.Scanner; +import exercises.Password; + +public class pruebaPassword { + + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + System.out.print("Numero de contraseņas a generar: "); + int n = s.nextInt(); + System.out.print("Introduce la longitud de las contraseņas: "); + int longi = s.nextInt(); + Password passwd = new Password(longi); + int cont = 0; + while (cont < n) { + passwd.setPass(Password.generarPassword(longi)); + System.out.print(passwd.getPass() + "\t Es fuerte: " + passwd.esFuerte(passwd.getPass()) + "\n"); + cont++; + } + } +} +