net.sf.easyweb4j.util
Class HashUtil

java.lang.Object
  extended by net.sf.easyweb4j.util.HashUtil

public class HashUtil
extends Object

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");
     }
 }
 

Author:
Chandra Sekar S

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

HashUtil

public HashUtil()
Method Detail

hash

public static String hash(String input,
                          String algorithm)
Same as hash(input, new byte[0], algorithm, null);

See Also:
hash(String, byte[], String, String)

hash

public static String hash(String input,
                          byte[] salt,
                          String algorithm)
Same as hash(input, salt, algorithm, null);

See Also:
hash(String, byte[], String, String)

hash

public static String hash(String input,
                          String algorithm,
                          String charsetName)
Same as hash(input, new byte[0], algorithm, charsetName);

See Also:
hash(String, byte[], String, String)

hash

public 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. The given algorithm is passed to MessageDigest and the given charSetName is used to extract independent bytes from the input String.

Parameters:
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.
Returns:
The generated hash.

generateSalt

public static byte[] generateSalt(int length)
Generates a random byte array of specified length. This can be passed to the hash(String, byte[], String, String) method. It uses SecureRandom with "SHA1PRNG" algorithm to generate the salt bytes.

Parameters:
length - The length of the generated salt.
Returns:
The salt.


Copyright © 2009-2010 Chandra Sekar S. All Rights Reserved.