Package com.philip.journal.login.service.util

Source Code of com.philip.journal.login.service.util.PasswordUtil

/**
* @Date: Feb 25, 2010 5:37:43 PM
*/
package com.philip.journal.login.service.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;
import org.jasypt.util.password.StrongPasswordEncryptor;

import com.philip.journal.core.Messages;
import com.philip.journal.core.bean.User;
import com.philip.journal.core.exception.JournalException;

/**
* TODO: Unit testing.
*
* @author cry30
*/
public final class PasswordUtil {

    /** Class logger instance. */
    private static Logger logger = Logger.getLogger(PasswordUtil.class); // NOPMD by r39

    /** Encyptor instance. */
    private static StrongPasswordEncryptor spe = new StrongPasswordEncryptor();

    /** Utility class cannot have public constructor. */
    private PasswordUtil() {}

    /** Encrypt key. Not sure if we can change this. */
    private static final int ENCRYPT_KEY = 0xFF;

    /** Singleton instance. */
    private static final PasswordUtil INSTANCE = new PasswordUtil();

    /**
     * Factory method.
     *
     * @return Singleton instance.
     */
    public static PasswordUtil getInstance()
    {
        return INSTANCE;
    }

    /**
     * Encrypts the password.
     *
     * @param user user entity.
     * @param password the raw password to encrypt.
     * @return encrypted password.
     */
    public String encrypt(final User user, final String password)
    {
        return spe.encryptPassword(sha512Encrypt(user, password));
    }

    /**
     * Null password is equivalent to empty String.
     *
     * @param user user entity.
     * @param password the raw password to encrypt.
     * @return encrypted password.
     *
     * @exception JournalException when user is null.
     */
    String sha512Encrypt(final User user, final String password)
    {
        if (user == null) {
            throw JournalException.wrapperException(new IllegalArgumentException(Messages.Error.IAE_NULL));
        }

        final StringBuilder passwordClone = new StringBuilder(password == null ? "" : password);
        passwordClone.append(user.getUsername());
        final byte[] defaultBytes = passwordClone.toString().getBytes();
        String retval = null;
        try {
            final MessageDigest algorithm = MessageDigest.getInstance("SHA-512");
            algorithm.reset();
            algorithm.update(defaultBytes);
            final byte[] messageDigest = algorithm.digest();

            final StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                hexString.append(Integer.toHexString(ENCRYPT_KEY & messageDigest[i]));
            }
            retval = hexString.toString();
        } catch (final NoSuchAlgorithmException nsae) {
            logger.debug(nsae.getMessage(), nsae);
        }
        return retval;
    }

    /**
     * Validates if the given password is valid.
     *
     * @param user user entity object.
     * @param password - the password to validate against the user.
     *
     * @return true if the password supplied is valid.
     */
    public boolean checkPassword(final User user, final String password)
    {
        return spe.checkPassword(sha512Encrypt(user, password), user.getPassword());
    }

    /**
     * For quick testing only.
     *
     * @param args command line arguments.
     */
    public static void main(final String[] args) {
        final StrongPasswordEncryptor bpe = new StrongPasswordEncryptor();

        logger.debug(bpe.encryptPassword("cry30"));
        final String passw1 = "cry3011111111111111111111111111111111111111111111111111111111"; // NOPMD by r39 on 3/30/11 1:29 PM
        final String passw2 = "cry301"; // NOPMD by r39 on 3/30/11 1:28 PM

        final String encrypted1 = bpe.encryptPassword(passw1);
        final String encrypted2 = bpe.encryptPassword(passw2);

        logger.debug(bpe.checkPassword(passw1, encrypted1));
        logger.debug(bpe.checkPassword(passw1, encrypted2));
        logger.debug(bpe.checkPassword(passw2, encrypted1));
        logger.debug(bpe.checkPassword(passw2, encrypted2));

        logger.debug(encrypted1.length());
        logger.debug(encrypted2.length());

    }

}
TOP

Related Classes of com.philip.journal.login.service.util.PasswordUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.