egovframework.rte.fdl.crypto.EgovPasswordLoad.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.crypto.EgovPasswordLoad.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovframework.rte.fdl.crypto;

import org.apache.log4j.Logger;
import org.jasypt.util.password.ConfigurablePasswordEncryptor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/*
*  , ?  ?  ?
* @author ??   
* @since 2009. 03.10
* @version 1.0
* @see <pre>
*  == ?(Modification Information) ==
*   
*   ?      ?           
*  -------    --------    ---------------------------
*   2009.03.10            ?
* 
* </pre>
*/

public class EgovPasswordLoad {
    /** Password  Class */
    ConfigurablePasswordEncryptor passwordEncryptor = null;
    private static Logger logger = CryptoLog.getLogger(EgovPasswordLoad.class);
    /** ApplicationContext */
    ApplicationContext context;
    /** crypto_config_xml  */
    String default_path = "classpath*:spring/context-config.xml";
    /** crytoConfig  Class */
    CryptoConfig cryptoConfig;

    /**
     * EgovPasswordLoad Class ??
     */
    public EgovPasswordLoad() {
        passwordEncryptor = new ConfigurablePasswordEncryptor();
        context = new FileSystemXmlApplicationContext(default_path);
        cryptoConfig = (CryptoConfig) context.getBean("config");
        String pwdAlgorithm = cryptoConfig.getPasswordAlgorithm();
        if (pwdAlgorithm.trim().length() <= 0)
            passwordEncryptor.setAlgorithm("SHA-1");
        else
            passwordEncryptor.setAlgorithm(pwdAlgorithm);
        passwordEncryptor.setPlainDigest(true);
    }

    /**
     * ?  
     * @param str -   
     * @return - ? ?
     */
    public String encrypt(String str) {
        // TODO Auto-generated method stub
        String rtn_str = passwordEncryptor.encryptPassword(str);
        return rtn_str;
    }

    /**
     * ?  ?
     * @param planPD - ? 
     * @param cryptoPD - ? 
     * @return true/false
     */
    public boolean checkPassword(String planPD, String cryptoPD) {
        return passwordEncryptor.checkPassword(planPD, cryptoPD);
    }

    /**
     * 
     * @param str -  ?
     */
    public void debug(String str) {
        logger.debug(str);
    }

    public static void main(String[] args) {
        EgovPasswordLoad epl = new EgovPasswordLoad();
        String str = epl.encrypt("Egov");
        epl.debug(str);

        if (epl.checkPassword("Egov", str))
            epl.debug("?.");
        else
            epl.debug("? .");
    }

}