-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
149 lines (131 loc) · 4.53 KB
/
User.java
File metadata and controls
149 lines (131 loc) · 4.53 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class User {
private int userID;
private String username;
private String password;
private String email;
private String dateOfBirth;
private String salt;
/**
* The constructor for the user class for the object that is stored inside the json file
* @param user The username of the user
* @param pass The password (in clear text) for the user - the password is encrypted later
* @param em The email address for the user
* @param dob
* @param filename
*/
public User(String user, String pass, String em, String dob, String filename) {
this.username = user;
this.password = pass;
this.email = em;
this.dateOfBirth = dob;
this.userID = setID(filename);
this.salt = generateSalt();
}
public JSONObject createJson() {
JSONObject obj = new JSONObject();
obj.put("ID", userID);
obj.put("Username", username);
obj.put("Password", password);
obj.put("Email", email);
obj.put("Date of Birth", dateOfBirth);
obj.put("Salt Key", salt);
return obj;
}
public String getUsername() {
return this.username;
}
public String getPass(){return this.password;}
public void hashPass(String pass) {
MessageDigest md;
String passToHash = pass+salt;
try {
md = MessageDigest.getInstance("SHA-512");
md.update(passToHash.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for(byte b : digest){
sb.append(String.format("%02x", b & 0xff));
}
this.password = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private int setID(String filename) {
int id = 10000;
File file = new File(filename);
if (file.length() == 0) {
return id;
}
else {
try (FileReader fr = new FileReader(filename)) {
JSONParser parser = new JSONParser();
Object obj = parser.parse(fr);
JSONArray array = (JSONArray)obj;
id += array.size();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return id;
}
public void submit(String filename){
JSONObject jObject = createJson();
JSONObject u = new JSONObject();
JSONArray array = new JSONArray();
u.put("Users", jObject);
File file = new File(filename);
if(file.length()==0){
try(FileWriter fw = new FileWriter(filename)) {
array.add(u);
fw.write(array.toJSONString());
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
else{
try(FileReader fr = new FileReader(filename)){
JSONParser parser = new JSONParser();
Object object = parser.parse(fr);
array = (JSONArray)object;
array.add(u);
try (FileWriter fw = new FileWriter(filename)){
fw.write(array.toJSONString());
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
catch(IOException e){e.printStackTrace();}
catch(org.json.simple.parser.ParseException e){e.printStackTrace();}
}
}
private String generateSalt(){
String charsToUse = "ABCDEFGHIJKLMONPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
int size = 16;
StringBuilder builder = new StringBuilder();
Random random = new Random();
for(int i =0; i < size; i++){
builder.append(charsToUse.charAt(random.nextInt(charsToUse.length())));
}
return builder.toString();
}
}