Profile.java
7.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package net.ziemers.swxercise.lg.model.user;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import javax.xml.bind.DatatypeConverter;
import net.ziemers.swxercise.db.BaseEntity;
import net.ziemers.swxercise.lg.user.enums.PasswordHashAlgorithm;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Random;
/**
* Verwaltet die Anmeldedaten eines anmeldefähigen Benutzers. Das Kennwort wird niemals im Klartext
* gespeichert, sondern stets nur in seiner verhashten Form.
*
* Copyright (c) 2017 Dipl.-Inform. Thomas Ziemer
*
* This file is part of swXercise.
*
* swXercise is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* swXercise is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with swXercise.
* If not, see <http://www.gnu.org/licenses/>.
*/
@Entity
public class Profile extends BaseEntity {
@NotNull
private String username;
@NotNull
private String passwordHash;
@NotNull
private PasswordHashAlgorithm hashAlgorithm = PasswordHashAlgorithm.SHA512;
@NotNull
private String salt;
private String mailaddress;
private Role role;
/*
* *****************************************************************************************************************************
* Konstruktoren
* *****************************************************************************************************************************
*/
public Profile() {
super();
setSalt(this.generateSalt());
}
public Profile(final String username, final String password) {
this();
setUsername(username);
setPassword(password);
}
/*
* *****************************************************************************************************************************
* Methoden
* *****************************************************************************************************************************
*/
private byte[] base64ToByte(final String str) {
byte[] bytes;
// Java 6 ships the javax.xml.bind.DatatypeConverter this class provides two static methods that support the same decoding &
// encoding: parseBase64Binary() and printBase64Binary(). Use this and you will not need an extra library, like Apache
// Commons Codec.
// BASE64Decoder decoder = new BASE64Decoder();
// try {
// bytes = decoder.decodeBuffer(str);
bytes = DatatypeConverter.parseBase64Binary(str);
// } catch(IOException e) {
// e.printStackTrace();
// }
return bytes;
}
private String byteToBase64(final byte[] bytes) {
// Java 6 ships the javax.xml.bind.DatatypeConverter this class provides two static methods that support the same decoding &
// encoding: parseBase64Binary() and printBase64Binary(). Use this and you will not need an extra library, like Apache
// Commons Codec.
// BASE64Encoder encoder = new BASE64Encoder();
// String str = encoder.encode(bytes);
String str = DatatypeConverter.printBase64Binary(bytes);
return str;
}
private String cryptString(String string) {
final int HASH_ITERATION_COUNT = 5;
String hashedString = "";
// vor dem Verschlüsseln den benutzerspezifischen Salt an das Kennwort anhängen
string += this.getSalt();
// Quelle: http://codeschnipsel.wordpress.com/2008/11/13/passwort-hash-in-java/
MessageDigest md;
try {
md = MessageDigest.getInstance(getHashAlgorithm().getAlgorithm());
md.reset();
md.update(base64ToByte(this.getSalt()));
byte[] bytes = md.digest(string.getBytes("UTF-8"));
for(int i = 0; i < HASH_ITERATION_COUNT; i++) {
md.reset();
bytes = md.digest(bytes);
}
hashedString = byteToBase64(bytes);
} catch(Exception e) {
e.printStackTrace();
}
return hashedString;
}
private String generateSalt() {
final int HASH_LENGTH = 20; // Ferguson & Schneier overcautiously recommend 32
// Quelle: http://www.javamex.com/tutorials/cryptography/pbe_salt.shtml
Random random = new SecureRandom();
byte[] salt = new byte[HASH_LENGTH];
random.nextBytes(salt);
return byteToBase64(salt);
}
/*
* *****************************************************************************************************************************
* Getters und Setters
* *****************************************************************************************************************************
*/
public String getUsername() {
return username;
}
private void setUsername(String username) {
this.username = username;
}
// da die Methode "private" ist, wird der Kennwort-Hashwert niemals per REST in einem JSON-Objekt übermittelt
private String getPasswordHash() {
return passwordHash;
}
/**
* Setzt das im Profil hinterlegte Kennwort. Es wird darin sicher verschlüsselt abgelegt.
*
* @param password das gewünschte Kennwort
*/
public void setPassword(String password) {
// das Klartextkennwort wird niemals gespeichert!
this.passwordHash = cryptString(password);
}
@SuppressWarnings("unused")
private void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
/**
* Vergleicht das übergebene Kennwort mit dem im Profil hinterlegten.
*
* @param password das Klartext-Kennwort, mit dem das im Profil hinterlegte verglichen werden soll
* @return <code>true</code>, wenn die Kennwörter identisch sind.
*/
public boolean isValidPassword(final String password) {
final String passwordHash = cryptString(password);
return getPasswordHash().equals(passwordHash);
}
@Enumerated(EnumType.STRING)
private PasswordHashAlgorithm getHashAlgorithm() {
return hashAlgorithm;
}
@SuppressWarnings("unused")
private void setHashAlgorithm(PasswordHashAlgorithm hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
// da die Methode "private" ist, wird der Kennwort-Saltwert niemals per REST in einem JSON-Objekt übermittelt
private String getSalt() {
return salt;
}
private void setSalt(String salt) {
this.salt = salt;
}
public String getMailaddress() {
return mailaddress;
}
public void setMailaddress(String mailaddress) {
this.mailaddress = mailaddress;
}
public Profile withMailaddress(final String mailaddress) {
setMailaddress(mailaddress);
return this;
}
@ManyToOne
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}