|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.easyweb4j.util.HashUtil
public class HashUtil
Utility class to handling hashing requirements of web applications, especially while storing passwords. Please don't store passwords as clear text, hashing them up is really simple.
Here is a simple code to hash a user's password.
public class User extends Model { private String passwordHash; private byte[] salt; private void hashPassword(String password) { salt = HashUtil.generateSalt(10); passwordHash = HashUtil.hash(password, salt, "md5", "UTF-8"); } }
Constructor Summary | |
---|---|
HashUtil()
|
Method Summary | |
---|---|
static byte[] |
generateSalt(int length)
Generates a random byte array of specified length. |
static String |
hash(String input,
byte[] salt,
String algorithm)
Same as hash(input, salt, algorithm, null); |
static String |
hash(String input,
byte[] salt,
String algorithm,
String charsetName)
Generates a has of the given input String by appending the given salt bytes. |
static String |
hash(String input,
String algorithm)
Same as hash(input, new byte[0], algorithm, null); |
static String |
hash(String input,
String algorithm,
String charsetName)
Same as hash(input, new byte[0], algorithm, charsetName); |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public HashUtil()
Method Detail |
---|
public static String hash(String input, String algorithm)
hash(input, new byte[0], algorithm, null);
hash(String, byte[], String, String)
public static String hash(String input, byte[] salt, String algorithm)
hash(input, salt, algorithm, null);
hash(String, byte[], String, String)
public static String hash(String input, String algorithm, String charsetName)
hash(input, new byte[0], algorithm, charsetName);
hash(String, byte[], String, String)
public static String hash(String input, byte[] salt, String algorithm, String charsetName)
input
- The String to be hashed.salt
- The salt to be added to the input.algorithm
- The hashing algorithm to be used.charsetName
- The character set to be used for extracting bytes from the
String.
public static byte[] generateSalt(int length)
hash(String, byte[], String, String)
method. It uses
SecureRandom
with "SHA1PRNG" algorithm to generate the salt
bytes.
length
- The length of the generated salt.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |