Java MD5 String md5Signature(TreeMap params, String secret)

Here you can find the source of md5Signature(TreeMap params, String secret)

Description

md Signature

License

Open Source License

Declaration

public static String md5Signature(TreeMap<String, String> params,
        String secret) 

Method Source Code

//package com.java2s;

import java.security.MessageDigest;

import java.util.Iterator;

import java.util.Map;
import java.util.TreeMap;

public class Main {

    public static String md5Signature(TreeMap<String, String> params,
            String secret) {/*from ww  w.  j  a v  a 2 s  .c o  m*/
        String result = null;
        StringBuffer orgin = getBeforeSign(params, new StringBuffer(secret));
        if (orgin == null)
            return result;
        orgin.append(secret);
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            result = byte2hex(md.digest(orgin.toString().getBytes("utf-8")));
        } catch (Exception e) {
            throw new java.lang.RuntimeException("sign error !");
        }
        return result;
    }

    private static StringBuffer getBeforeSign(
            TreeMap<String, String> params, StringBuffer orgin) {
        if (params == null)
            return null;
        Map<String, String> treeMap = new TreeMap<String, String>();
        treeMap.putAll(params);
        Iterator<String> iter = treeMap.keySet().iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next();
            orgin.append(name).append(params.get(name));
        }
        return orgin;
    }

    private static String byte2hex(byte[] b) {
        StringBuffer hs = new StringBuffer();
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs.append("0").append(stmp);
            } else {
                hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }
}

Related

  1. md5Hex(String s)
  2. md5hex(String source)
  3. md5Hex(String str)
  4. md5raw(String data)
  5. md5Representation(String data)
  6. md5Signature(TreeMap params, String secret)
  7. md5String(String input)
  8. md5String(String key)
  9. md5String(String paramString)