Profile.java
5.65 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
package net.ziemers.swxercise.lg.model.user;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
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;
@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;
/*
* *****************************************************************************************************************************
* 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;
}
public 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;
}
private void setPassword(String password) {
// das Klartextkennwort wird niemals gespeichert!
this.passwordHash = cryptString(password);
}
private void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
public boolean isValidPassword(final String password) {
final String passwordHash = cryptString(password);
return getPasswordHash().equals(passwordHash);
}
@Enumerated(EnumType.STRING)
private PasswordHashAlgorithm getHashAlgorithm() {
return hashAlgorithm;
}
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;
}
}