-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordManager.java
More file actions
129 lines (113 loc) · 3.33 KB
/
Copy pathPasswordManager.java
File metadata and controls
129 lines (113 loc) · 3.33 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
public class PasswordManager
{
public static void main(String[] args)
{
AddOrChangePassword();
CheckPassword();
System.console().readLine();
}
public static void AddOrChangePassword()
{
//input user name
System.out.println("New user and password...");
System.out.println("Enter your new user name");
String username = System.console().readLine();
if (username == null || username.length() == 0)
{
System.out.println("User name cannot be empty. Try again.");
//try again
AddOrChangePassword();
return;
}
//input password
System.out.println("Enter your new password:");
String password = System.console().readLine();
//Validate password criteria
if (password == null || password.length() == 0)
{
System.out.println("Password cannot be empty. Try again.");
//try again
AddOrChangePassword();
return;
}
else if (password.length() < 6)
{
System.out.println("Should be at least 6 chars. Try again.");
//try again
AddOrChangePassword();
return;
}
//Calculate hash
MessageDigest md = null;
try{
md = MessageDigest.getInstance("MD5");
} catch ( NoSuchAlgorithmException ex ) {
throw new IllegalStateException( ex );
}
byte[] hash = null;
try{
hash = md.digest(password.getBytes("UTF-8"));//MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
} catch ( java.io.UnsupportedEncodingException ex ) {
throw new IllegalStateException( ex );
}
//Save password
try {
FileOutputStream stream = new FileOutputStream(username);
try {
stream.write(hash);
stream.close();
}catch ( IOException inner ) {
throw new IllegalStateException( inner );
}
} catch ( java.io.FileNotFoundException ex ) {
throw new IllegalStateException( ex );
}
}
public static void CheckPassword()
{
//input user name and password
System.out.println("Check Password...");
System.out.println("Enter your existing user name");
String username = System.console().readLine();
System.out.println("Enter your existing password:");
String password = System.console().readLine();
//Calculate hash
MessageDigest md = null;
try{
md = MessageDigest.getInstance("MD5");
} catch ( NoSuchAlgorithmException ex ) {
throw new IllegalStateException( ex );
}
byte[] hash = null;
try{
hash = md.digest(password.getBytes("UTF-8"));//MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
} catch ( java.io.UnsupportedEncodingException ex ) {
throw new IllegalStateException( ex );
}
byte[] savedHash = null;
try
{
savedHash = Files.readAllBytes(new File(username).toPath());
}
catch (Exception ex)
{ /*gulp*/}
if (hash == null || savedHash == null || java.util.Arrays.equals(savedHash, hash))
{
System.out.println("Correct password!");
}
else
{
System.out.println("Incorrect password!");
}
}
}