php 参数排序 计算sign_参数排序,生成md5的sign

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.regex.Matcher;

import java.util.regex.Pattern;public classHashUtils {private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e','f'};public static char[] encodeHex(byte[] bytes) {char[] chars = new char[32];for (int i = 0; i < chars.length; i += 2) {byte b = bytes[(i / 2)];

chars[i]= HEX_CHARS[(b >>> 4 & 0xF)];

chars[(i+ 1)] = HEX_CHARS[(b & 0xF)];

}returnchars;

}public staticMessageDigest getDigest(String algorithm) {try{returnMessageDigest.getInstance(algorithm);

}catch(NoSuchAlgorithmException ex) {throw new IllegalStateException("Could not find MessageDigest with algorithm \"" + algorithm + "\"", ex);

}

}public staticString md5(String src) {

MessageDigest md5= getDigest("MD5");return newString(encodeHex(md5.digest(src.getBytes())));

}public static String toSign(Mapmap) {return "";

};public static String getFormatParams(Map params,boolean flag) throws UnsupportedEncodingException {

List> infoIds =

new ArrayList>((Collection extends Entry>) params.entrySet());

Collections.sort(infoIds,new Comparator>() {public int compare(Map.Entry arg0, Map.Entryarg1) {return(arg0.getKey()).compareTo(arg1.getKey());

}

});

String ret= "";for (Map.Entryentry : infoIds) {

ret+=entry.getKey();

ret+= "=";if(flag) {if(generateJudgment(entry.getValue().toString())) {

ret+=URLEncoder.encode(entry.getValue().toString(), "UTF-8") ;

}else{

ret+=entry.getValue().toString();

}

}else{

ret+=entry.getValue().toString();

}

ret+= "&";

}

ret= ret.substring(0, ret.length() - 1);returnret;

}//判断是否有汉字

public staticboolean generateJudgment(String countname){

Pattern p= Pattern.compile("[\u4e00-\u9fa5]");

Matcher isNum=p.matcher(countname);if(isNum.find()) {return true;

}return false;

}public static voidmain(String[] args) {

String stringToSign= "B856915E" + "57C8666D91C346D1A23C766A4B8EDA85" + "1584338529869";

System.out.println(md5(stringToSign));

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值