Monday, December 16, 2013

[Java] Encryption Using SHA-256

When there is a requirement to save password to a database, there is always a need for this password to be secured. One way of securing the password is to encrypt it. Below is a sample password encryption class using SHA-256.


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;
import org.jboss.security.auth.spi.Util;

public class EncryptionUtil 
{
 
 private final static Logger LOG = Logger.getLogger(EncryptionUtil .class .getName()); 
 
 /**
  * Encrypt with SHA-256 and BASE64.
  * @param str String to be encrypted.
  * @return    Encrypted string.
  */
 public static String encrypt(String password) 
 {
      return Util.createPasswordHash("SHA-256", "BASE64", null, null,password);
 }

}//end class

No comments:

Post a Comment