php签名验证
function getSign($params){
$appSecret="abcsd";
// 1. 对加密数组进行字典排序
foreach ($params as $key=>$value){
$arr[$key] = $key;
}
sort($arr,SORT_STRING);
// var_dump($arr);
$str = $appSecret;
foreach ($arr as $k => $v) {
$str = $str.$arr[$k].$params[$v];
}
$restr = $str.$appSecret;
$sign = strtoupper(md5($restr));
return $sign;
}
$params=[];
$params["aaaaaaa"]="sss";
$params["ava"]="sss";
$params["aba"]="sss";
$params["1"]="3rd";
$params["qidong"]="123";
$params["99"]="sss";
java签名
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Main {
public static void main(String[] args) {
String appSecret = "abcsd";
HashMap< String, String> hMap = new HashMap< String, String>();
hMap.put("aaaaaaa","sss");
hMap.put("ava","sss");
hMap.put("aba","sss");
hMap.put("1", "3rd");
hMap.put("qidong","123");
hMap.put("99","sss");
Set set=hMap.keySet();
Object[] arr=set.toArray();
Arrays.sort(arr);
String str = appSecret;
for (int i = 0;i<arr.length;i++){
str=str+arr[i]+hMap.get(arr[i]);
}
str=str+appSecret;
// System.out.print(str);
String result=MD5(str);
System.out.print(result.toUpperCase());
}
private static String MD5(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
// System.out.println("MD5(" + sourceStr + ",32) = " + result);
// System.out.println("MD5(" + sourceStr + ",16) = " + buf.toString().substring(8, 24));
} catch (NoSuchAlgorithmException e) {
System.out.println(e);
}
return result;
}
}
One comment
不错不错,我喜欢看 www.jiwenlaw.com