MD5(3)

import java.io.UnsupportedEncodingException;

import java.security.PrivateKey;

import java.security.SignatureException;

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

/**

 * MD5签名处理核心文件

 * */

public class MD5 {

    /**

     * 签名字符串

     *

     * @param text

     *            需要签名的字符串

     * @param key

     *            密钥

     * @param input_charset

     *            编码格式

     * @return 签名结果

     */

    public static String sign(String text, String key, String charset) throws Exception {

        text = text + key;

        return DigestUtils.md5Hex(getContentBytes(text, charset));

    }

    /**

     * 签名字符串

     *

     * @param text

     *            需要签名的字符串

     * @param key

     *            密钥

     * @param input_charset

     *            编码格式

     * @return 签名结果

     * @deprecated 无替代方法

     */

    public static String sign(String text, PrivateKey key, String charset) throws Exception {

        throw new UnsupportedOperationException();

    }

    /**

     * 签名字符串

     *

     * @param text

     *            需要签名的字符串

     * @param sign

     *            签名结果

     * @param key

     *            密钥

     * @param input_charset

     *            编码格式

     * @return 签名结果

     */

    public static boolean verify(String text, String sign, String key, String charset)

                                                                                      throws Exception {

        text = text + key;

        String mysign = DigestUtils.md5Hex(getContentBytes(text, charset));

        if (mysign.equals(sign)) {

            return true;

        } else {

            return false;

        }

    }

    /**

     * @param content

     * @param charset

     * @return

     * @throws SignatureException

     * @throws UnsupportedEncodingException

     */

    private static byte[] getContentBytes(String content, String charset) {

        if (charset == null || "".equals(charset)) {

            return content.getBytes();

        }

        try {

            return content.getBytes(charset);

        } catch (UnsupportedEncodingException e) {

            throw new RuntimeException("签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);

        }

    }

}

 

posted on 2019-02-19 13:59  我是司  阅读(203)  评论(0编辑  收藏  举报

导航