码迷,mamicode.com
首页 > 其他好文 > 详细

21:数据拼接MD5加密

时间:2018-08-15 12:02:25      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:throw   hashmap   length   utf-8   密钥   mes   ++   codec   throwable   

1:数据拼接

public RequestParams makeDataSearch(RequestParams mRequestParams){
        String[][] detaDetail = { { "amount", amount }, {"app_id", appid},{ "auth_code", payCode }, 
                { "busi_code", mSettings.getValue("storeId") },
                { "dev_id",  mSettings.getValue("terminalId") }, { "down_trade_no", orderNo }, { "good_details", "aa" }, { "oper_id", "01" },
                { "raw_data", "11" }, { "undiscountable_amount", amount }, { "subject", "aa" }, {"timestamp", timestamp}, {"version", "v43"}};
        return MakeDataDetail(detaDetail, mRequestParams);
    }

public RequestParams MakeDataDetail(String[][] dataDeatil, RequestParams mRequestParams) {
        HashMap<String, String> params = new HashMap<String, String>();
        for (int i = 0; i < dataDeatil.length; i++) {
            params.put(dataDeatil[i][0], dataDeatil[i][1]);// 获取sign使用
            mRequestParams.addBodyParameter(dataDeatil[i][0], dataDeatil[i][1]);
        }
        StringBuilder data = new StringBuilder((params.size() + 1) * 10);
        buildPayParams(data, params, false);
        String dataWithMd5 = Md5Utils.sign(data.append("&APP_SECRET=BTOOuffLDoVTPZsgDZgGapXsoBeKvTMT").toString(), "", "UTF-8").toUpperCase();
        mRequestParams.addBodyParameter("sign", dataWithMd5);
        return mRequestParams;
    }

public void buildPayParams(StringBuilder sb, Map<String, String> payParams, boolean encoding) {
        List<String> keys = new ArrayList<String>(payParams.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            sb.append(key).append("=");
            if (encoding) {
                sb.append(urlEncode(payParams.get(key)));
            } else {
                sb.append(payParams.get(key));
            }
            sb.append("&");
        }
        sb.setLength(sb.length() - 1);
    }

    public String urlEncode(String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (Throwable e) {
            return str;
        }
    }

 

 

2:MD5加密

public class Md5Utils{

    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + key;
        return org.apache.commons.codec.digest.
                DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }
    
    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param sign 签名结果
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static boolean verify(String text, String sign, String key, String input_charset) {
        text = text + key;
        String mysign = org.apache.commons.codec.digest.
                  DigestUtils.md5Hex(getContentBytes(text, input_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("Md5Utils签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }

 

21:数据拼接MD5加密

标签:throw   hashmap   length   utf-8   密钥   mes   ++   codec   throwable   

原文地址:https://www.cnblogs.com/wnpp/p/9480483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!