forked from shivaylamba/Hacktoberfest
-
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.
Merge pull request shivaylamba#639 from PedroCisnerosSantana/master
Added PasswordGenerator in java
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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,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); | ||
} | ||
} |
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,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++; | ||
} | ||
} | ||
} | ||
|