com.greenline.guahao.biz.manager.payment.util.AlipayMd5Encrypt.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.guahao.biz.manager.payment.util.AlipayMd5Encrypt.java

Source

/*
 * Project: guahao-portal-biz-core
 * 
 * File Created at 2012-5-16
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.guahao.biz.manager.payment.util;

import java.io.UnsupportedEncodingException;
import java.security.SignatureException;

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

/**
 * ?MD5????? 3.1 2010-11-01 
 * ????????,???
 * ??????
 * */

public class AlipayMd5Encrypt {

    /**
     * MD5??
     * 
     * @param text 
     * 
     * @return 
     */
    public static String md5(String text, String charset) {
        return DigestUtils.md5Hex(getContentBytes(text, charset));
    }

    /**
     * 
     * @param content
     * @param charset
     * @return byte
     * @throws SignatureException
     * @throws UnsupportedEncodingException
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (StringUtils.isBlank(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(
                    "MD5??,??,??:"
                            + charset);
        }
    }

}