nanshen.utils.EncryptUtils.java Source code

Java tutorial

Introduction

Here is the source code for nanshen.utils.EncryptUtils.java

Source

/*
 * @(#)EncryptUtils.java, 2014-12-23.
 *
 * Copyright 2014 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package nanshen.utils;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * ??
 *
 * @author WANG Minghao
 */
public class EncryptUtils {

    private static final char PASSWORD_SEPARATOR = '|';

    /**
     * ??Salt?????
     *
     * @param rawPass
     * @return
     */
    public static String encodePassword(String rawPass) {
        String salt = generateSalt();
        return encodePassword(rawPass, salt);
    }

    private static String generateSalt() {
        return "1d312ye2shd12q12dfsd";
    }

    private static String encodePassword(String rawPass, String salt) {
        return DigestUtils.md5Hex(rawPass + salt) + PASSWORD_SEPARATOR + salt;
    }

    /**
     * ???
     * <p />
     * <b>?</b>?{@link #encodePassword(String, String)}?
     *
     * @param encPass 
     * @param rawPass ?
     * @return
     */
    public static boolean isPasswordValid(String encPass, String rawPass) {
        int index = encPass.indexOf(PASSWORD_SEPARATOR);
        if (index < 0) {
            return false;
        }
        String salt = encPass.substring(index + 1);
        return encPass.equals(encodePassword(rawPass, salt));
    }

}