-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword generator
More file actions
35 lines (24 loc) · 1.03 KB
/
Password generator
File metadata and controls
35 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.petras;
import java.security.SecureRandom;
import java.util.Random;
import java.util.Scanner;
public class Main {
private static final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*_=+-/";
public static void main(String[] args) {
Scanner Scanner = new Scanner(System.in);
System.out.println("Please Enter the lenght of your desired password:");
int length = Scanner.nextInt();
String password = generatePassword(length);
System.out.println("Random Password: " + password);
}
private static String generatePassword(int length) {
StringBuilder password = new StringBuilder(length);
Random random = new SecureRandom();
// Generate the required number of characters from character set
for (int i = 0; i < length ; i++) {
int randomIndex = random.nextInt(ALPHA_CAPS.length());
password.append(ALPHA_CAPS.charAt(randomIndex));
}
return new String(password);
}
}