-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashPassword.java
More file actions
129 lines (102 loc) · 3.81 KB
/
HashPassword.java
File metadata and controls
129 lines (102 loc) · 3.81 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.security.SecureRandom;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
public class HashPassword extends Password implements Encryptable {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// test the class
System.out.println("Testing password: TestPassword1");
HashPassword hp = new HashPassword("TestPassword1");
// should print true
System.out.println(hp.checkPassword("TestPassword1"));
// should print false
System.out.println(hp.checkPassword("TestPassword2"));
// generate new pass
hp.generatePass(12);
// test generated password
Scanner scanner = new Scanner(System.in);
System.out.println("Enter generated password:");
String response = scanner.nextLine();
System.out.println(hp.checkPassword(response));
// test getDate
System.out.println(hp.getDate());
} // end main
public HashPassword(String pass) {
// create random salt
SecureRandom rand = new SecureRandom();
this.salt = new byte[64];
rand.nextBytes(this.salt);
// set dateSet to current date
this.dateSet = new Date();
// salt and hash the password
this.password = this.encrypt(pass);
} // end constructor
public void setPassword(String newPass) {
// set dateSet to current date
this.dateSet = new Date();
// salt and hash the password
this.password = this.encrypt(newPass);
} // end setPassword
public boolean checkPassword(String pass) {
boolean valid = false;
byte[] encPass;
try {
PBEKeySpec spec = new PBEKeySpec(pass.toCharArray(), this.salt, 65536, 128);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
encPass = factory.generateSecret(spec).getEncoded();
} catch (Exception e) {
System.out.println("Error: something went wrong while checking the password");
// TODO write to error log
encPass = null;
} // end try catch
if (Arrays.equals(this.password, encPass)) {
valid = true;
} // end if
return valid;
} // checkPassword
public void generatePass(int length) {
PasswordGenerator generator = new PasswordGenerator();
String newPass = generator.generate(length);
System.out.println("Generated Password:");
System.out.println(newPass);
Scanner scanner = new Scanner(System.in);
boolean keepGoing = true;
while (keepGoing) {
System.out.println("\nWould you like to set this as your password? (y/n)");
String response = scanner.nextLine();
if (response.toUpperCase().equals("Y")) {
this.setPassword(newPass);
System.out.println("\nPassword successfully changed");
System.out.println("\nPress enter to continue");
scanner.nextLine();
keepGoing = false;
} else if (response.toUpperCase().equals("N")) {
System.out.println("\nPassword change cancelled");
System.out.println("\nPress enter to continue");
scanner.nextLine();
keepGoing = false;
} else {
System.out.println("\nError: Please enter either 'y' or 'n'");
} // end if else
} // end while
} // end generatePass
public Date getDate() {
return this.dateSet;
} // end getDate
public byte[] encrypt(String pass) {
byte[] encPass;
try {
PBEKeySpec spec = new PBEKeySpec(pass.toCharArray(), this.salt, 65536, 128);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
encPass = factory.generateSecret(spec).getEncoded();
} catch (Exception e) {
// TODO write to error log
System.out.println("Error: something went wrong encrypting the password");
encPass = null;
} // end try catch
return encPass;
}
} // end HashPassword