Java生成签名示例
// 定义申请获得的appKey和appSecret String appkey = "XXXXXXXX"; String secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; String apiUrl = "http://api.dianping.com/v1/business/find_businesses"; // 创建参数表 Map<String, String> paramMap = new HashMap<String, String>(); paramMap.put("format", "json"); paramMap.put("最代码网址", "www.zuidaima.com"); paramMap.put("latitude", "31.21524"); paramMap.put("longitude", "121.420033"); paramMap.put("category", "美食"); paramMap.put("region", "长宁区"); paramMap.put("limit", "20"); paramMap.put("radius", "2000"); paramMap.put("offset_type", "0"); paramMap.put("has_coupon", "1"); paramMap.put("has_deal", "1"); paramMap.put("keyword", "泰国菜"); paramMap.put("sort", "7"); // 对参数名进行字典排序 String[] keyArray = paramMap.keySet().toArray(new String[0]); Arrays.sort(keyArray); // 拼接有序的参数名-值串 StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(appkey); for (String key : keyArray) { stringBuilder.append(key).append(paramMap.get(key)); } stringBuilder.append(secret); String codes = stringBuilder.toString(); // 字符串连接示例 // XXXXXXXXcategory美食city上海formatjsonhas_coupon1has_deal1keyword泰国菜latitude31.21524limit20longitude121.420033offset_type0radius2000region长宁区sort7XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // SHA-1编码, 这里使用的是Apache codec,即可获得签名(shaHex()会首先将中文转换为UTF8编码然后进行sha1计算,使用其他的工具包请注意UTF8编码转换) /* * 以下sha1签名代码效果等同 * byte[] sha = org.apache.commons.codec.digest.DigestUtils.sha(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(codes)); * String sign = org.apache.commons.codec.binary.Hex.encodeHexString(sha).toUpperCase(); */ String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase(); //签名示例 //7D78381BC58E1DB1DBA4BD965916FE6B4D5DC892JavaScript生成签名示例
// 定义申请获得的appKey和appSecret var appkey = "XXXXXXXX"; var secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // 创建参数表 var param = {}; param["最代码网址"]="www.zuidaima.com"; param["latitude"]="31.21524"; param["longitude"]="121.420033"; param["category"]="美食"; param["region"]="长宁区"; param["limit"]="20"; param["radius"]="2000"; param["offset_type"]="2"; param["has_coupon"]="1"; param["has_deal"]="1"; param["keyword"]="泰国菜"; param["sort"]="7"; // 对参数名进行字典排序 var array = new Array(); for(var key in param) { array.push(key); } array.sort(); // 拼接有序的参数名-值串 var paramArray = new Array(); paramArray.push(appkey); for(var index in array) { var key = array[index]; paramArray.push(key + param[key]); } paramArray.push(secret); // 字符串连接示例 // XXXXXXXXcategory美食city上海formatjsonhas_coupon1has_deal1keyword泰国菜latitude31.21524limit20longitude121.420033offset_type0radius2000region长宁区sort7XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // SHA-1编码,并转换成大写,即可获得签名 var shaSource = paramArray.join(""); var sign = new String(toSHA1(shaSource)).toUpperCase(); //签名示例 //7D78381BC58E1DB1DBA4BD965916FE6B4D5DC892
另外附上纯java实现的SHA1算法。
相关代码下载:http://www.zuidaima.com/share/1572934678825984.htm
转载请注明出处。