diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-10-25 19:03:06 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-10-25 19:13:19 +0100 |
| commit | 675f796447676ca7050a7a53360c50a0e083f2ae (patch) | |
| tree | 4f680852b4b30d8a56333cb99e072d10ed6da384 /src/de/fhswf/in | |
| parent | 151e21577446d182913cdb8b45a94da3178b0dce (diff) | |
| download | FIT-675f796447676ca7050a7a53360c50a0e083f2ae.tar.gz FIT-675f796447676ca7050a7a53360c50a0e083f2ae.zip | |
Add salt and hash function for passwords
Diffstat (limited to 'src/de/fhswf/in')
| -rw-r--r-- | src/de/fhswf/in/inf/fit/aufgabe3/LoginServlet.java | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/fit/aufgabe3/LoginServlet.java b/src/de/fhswf/in/inf/fit/aufgabe3/LoginServlet.java index 3106ecb..c5900c7 100644 --- a/src/de/fhswf/in/inf/fit/aufgabe3/LoginServlet.java +++ b/src/de/fhswf/in/inf/fit/aufgabe3/LoginServlet.java @@ -2,6 +2,8 @@ package de.fhswf.in.inf.fit.aufgabe3; import java.io.IOException; import java.io.PrintWriter; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; @@ -81,4 +83,38 @@ public class LoginServlet extends HttpServlet pw.println("</body>"); pw.println("</html>"); } + + /** + * Generate a SHA-1 encoded 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. + */ + public static String createSaltedPasswordHash(String password, String salt) + { + if (password == null) + { + throw new IllegalArgumentException("Password can't be null"); + } + + if (salt == null) + { + throw new IllegalArgumentException("Salt can't be null"); + } + + try + { + MessageDigest md = MessageDigest.getInstance("SHA-1"); + md.update((password + salt).getBytes()); + return new String(md.digest()); + } + catch (NoSuchAlgorithmException e) + { + throw new IllegalStateException( + "SHA-1 for some reason is not supported.", e); + } + } } |
