import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        try {
            String secret = "{{secret}}"; //用户秘钥
            String topic = "{{topic}}"; //消息主题
            String pub_time = "{{pub-time}}"; //原消息发布时间
            String data = "{{data-2}}"; //消息内容
            Map<String, String> map = new HashMap<String, String>();
            map.put("topic", topic);
            map.put("pub_time", pub_time);
            map.put("data", data);
            List<String> keys = new ArrayList(map.keySet()); //SET转LIST
            Collections.sort(keys); //排序
            StringBuffer sb = new StringBuffer(secret); //拼接签名原串
            for (String key : keys) {
                sb.append(key + map.get(key));
            }
            sb.append(secret);
            String sign = CalculateMd5(sb.toString()); //签名计算
            System.out.println(sign);
        } catch (Exception e) {
        }
    }

    private static String CalculateMd5(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(content.getBytes());
            byte[] digest = md.digest();
            Formatter formatter = new Formatter();
            for (byte b : digest) {
                formatter.format("%02x", b);
            }
            String result = formatter.toString();
            return result;
        } catch (Exception e) {
        }
        return null;
    }
}
