FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Others/PasswordGen.java at master · primary-code-test/Java · GitHub
primary-code-test
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Others
/
PasswordGen.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (35 loc) · 1.24 KB
Breadcrumbs
Java
/
Others
/
PasswordGen.java
Copy path
File metadata and controls
44 lines (35 loc) · 1.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
36
37
38
39
40
41
42
43
44
package
Others
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
List
;
import
java
.
util
.
Random
;
/**
* Creates a random password from ASCII letters Given password length bounds
*
* @author AKS1996
* @date 2017.10.25
*/
class
PasswordGen
{
public
static
void
main
(
String
args
[]) {
String
password
=
generatePassword
(
8
,
16
);
System
.
out
.
print
(
"Password: "
+
password
);
}
static
String
generatePassword
(
int
min_length
,
int
max_length
) {
Random
random
=
new
Random
();
String
upper
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
;
String
lower
=
"abcdefghijklmnopqrstuvwxyz"
;
String
numbers
=
"0123456789"
;
String
specialChars
=
"!@#$%^&*(){}?"
;
String
allChars
=
upper
+
lower
+
numbers
+
specialChars
;
List
<
Character
>
letters
=
new
ArrayList
<
Character
>();
for
(
char
c
:
allChars
.
toCharArray
())
letters
.
add
(
c
);
// Inbuilt method to randomly shuffle a elements of a list
Collections
.
shuffle
(
letters
);
StringBuilder
password
=
new
StringBuilder
();
// Note that size of the password is also random
for
(
int
i
=
random
.
nextInt
(
max_length
-
min_length
) +
min_length
;
i
>
0
; --
i
) {
password
.
append
(
letters
.
get
(
random
.
nextInt
(
letters
.
size
())));
}
return
password
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL