blob: 17c493624f94fbf11501105579ae8a13a2a8334e (
plain)
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
|
package de.fhswf.in.inf.fit.aufgabe5.model;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* The persistent class for the account database table.
*
*/
@Entity
@Table(name = "account")
@NamedQuery(name = "Account.findAll", query = "SELECT a FROM Account a")
public class Account implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "Username")
private String username;
@Column(name = "Password")
private String password;
@Column(name = "Salt")
private String salt;
public Account()
{
}
public String getUsername()
{
return this.username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return this.password;
}
public void setPassword(String password)
{
this.password = createSaltedPasswordHash(password);
}
private String getSalt()
{
// TODO Generate Salt if not set
return this.salt;
}
public Boolean isPasswordCorrect(String password)
{
return getPassword().equals(createSaltedPasswordHash(password));
}
/**
* Generate a Base64 encoded SHA-1 hashed password that is salted.
*
* @param password
* The password to encode.
* @param salt
* The salt for salting the password.
* @return The salted and encoded password hash.
*/
protected String createSaltedPasswordHash(String password)
{
try
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update((password + getSalt()).getBytes());
return Base64.getEncoder().encodeToString(md.digest());
}
catch (NoSuchAlgorithmException e)
{
throw new IllegalStateException(
"SHA-1 for some reason is not supported.", e);
}
}
}
|