Skip to content

Commit

Permalink
Merge pull request shivaylamba#639 from PedroCisnerosSantana/master
Browse files Browse the repository at this point in the history
Added PasswordGenerator in java
  • Loading branch information
shivaylamba authored Oct 5, 2020
2 parents 0ab9550 + 8041233 commit 402d6c2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
52 changes: 52 additions & 0 deletions PasswordGenerator_java/Password.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions PasswordGenerator_java/pruebaPassword.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}

0 comments on commit 402d6c2

Please sign in to comment.