字典排序MD5生成代码

/*
 * Project: payment.framework
 *
 * File Created at 2017年12月23日
 *
 * Copyright 2016 CMCC Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ZYHY Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license.
 */
package com.cmcc.hybj.payment.framework.https;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Comparator;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author gy
 * @Type Sign.java
 * @Desc 支付签名
 * @date 2017年12月23日 上午9:56:04
 */
public class Sign {
    private static Logger log = LoggerFactory.getLogger(Sign.class);

    /**
     * // 服务器端生成签名
     *
     * @param map
     * @return
     */
    public static String buildSign(Hashtable<String, String> map) {
        Set<String> keys = new TreeSet<String>(new MyComparator());
        Enumeration<String> enumeration = map.keys();
        while (enumeration.hasMoreElements()) {
            String keyss = enumeration.nextElement();
            keys.add(keyss);
        }
        // 拼接有序的参数名-值串
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : keys) {
            String value = map.get(key);
            if (value == null) {
                continue;
            } else if ("".equals(value.trim())) {
                continue;
            } else if ("sign".equals(key.trim())) {
                continue;
            } else {
                stringBuilder.append(key).append("=").append(value).append("&");
            }
        }
        String s = stringBuilder.toString();
        s = s.substring(0, s.length() - 1);
        log.info("排序后的参数 :" + s);
        return s;
    }

    /**
     * 内部类
     *
     * @author gy
     */
    public static class MyComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {

            return o1.compareTo(o2);// 升序排列
        }

    }
}

/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2017年12月23日 Administrator creat
 */

转载于:https://www.cnblogs.com/gyadmin/p/8308436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值