From f8d050310b2eed163f2365928d63611edbe3e4b1 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 16 Nov 2015 10:05:09 +0100 Subject: Put logic into Account class --- .../fhswf/in/inf/fit/aufgabe5/model/Account.java | 104 ++++++++++++++------- .../fhswf/in/inf/fit/aufgabe5/model/Account_.java | 2 +- 2 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/de/fhswf/in/inf/fit/aufgabe5/model/Account.java b/src/de/fhswf/in/inf/fit/aufgabe5/model/Account.java index 539353c..17c4936 100644 --- a/src/de/fhswf/in/inf/fit/aufgabe5/model/Account.java +++ b/src/de/fhswf/in/inf/fit/aufgabe5/model/Account.java @@ -1,54 +1,94 @@ package de.fhswf.in.inf.fit.aufgabe5.model; import java.io.Serializable; -import javax.persistence.*; +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; +@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; + @Id + @Column(name = "Username") + private String username; - @Column(name="Password") - private String password; + @Column(name = "Password") + private String password; - @Column(name="Salt") - private String salt; + @Column(name = "Salt") + private String salt; - public Account() { - } + public Account() + { + } - public String getUsername() { - return this.username; - } + public String getUsername() + { + return this.username; + } - public void setUsername(String username) { - this.username = username; - } + public void setUsername(String username) + { + this.username = username; + } - public String getPassword() { - return this.password; - } + public String getPassword() + { + return this.password; + } - public void setPassword(String password) { - this.password = password; - } + public void setPassword(String password) + { + this.password = createSaltedPasswordHash(password); + } - public String getSalt() { - return this.salt; - } + private String getSalt() + { + // TODO Generate Salt if not set + return this.salt; + } - public void setSalt(String salt) { - this.salt = 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); + } + } } \ No newline at end of file diff --git a/src/de/fhswf/in/inf/fit/aufgabe5/model/Account_.java b/src/de/fhswf/in/inf/fit/aufgabe5/model/Account_.java index e7da7b9..b9641d9 100644 --- a/src/de/fhswf/in/inf/fit/aufgabe5/model/Account_.java +++ b/src/de/fhswf/in/inf/fit/aufgabe5/model/Account_.java @@ -4,7 +4,7 @@ import javax.annotation.Generated; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.StaticMetamodel; -@Generated(value="Dali", date="2015-11-09T01:25:51.914+0100") +@Generated(value="Dali", date="2015-11-16T09:57:36.089+0100") @StaticMetamodel(Account.class) public class Account_ { public static volatile SingularAttribute username; -- cgit v1.2.3-70-g09d2