1
回答
1号店api签名算法的意义
科大讯飞通用文字识别100000次/天免费使用。立即申请   

1号店API官方文档:http://openapi.yihaodian.com/forward/inshop/apiSign.html

2-1、md5:将secret同时拼接到参数字符串头、尾部进行md5加密,格式是:byte2hex(md5(secretkey1value1key2value2...secret))

这里我有一点不是很明白,md5加密以后不是可以认为是不可逆的吗?

为什么要把参数也md5了?我之前只把密码用md5加密过。

那么这些参数, 如果拿不到,有什么意义呢?

求教各位大神。

<无标签>
举报
David_Lee_
发帖于5年前 1回/783阅
共有1个答案 最后回答: 5年前

你只看了一部分,没看完。在下面的方法中传递了原始参数的,同时也传了这个sign,到服务器上会再次验证比对的。

params.add(new BasicNameValuePair(key, treeMap.get(key)));
//上面就是原始的参数k-v对
public class PostClient {
	
		private static String POST_URL = "http://openapi.1mall.com/forward/api/rest/router";
		
		public static String sendByPost(Map<String, String> appParamMap, String secretKey, String urlStr ) {
			try {
	
				HttpClient httpClient = new DefaultHttpClient();
				if(urlStr == null || urlStr.trim().length() == 0){
					urlStr = POST_URL ;
				}
				
				HttpPost httpPost = new HttpPost(urlStr);
	
				TreeMap<String, String> treeMap = new TreeMap<String, String>();
				if (appParamMap != null) {
					treeMap.putAll(appParamMap);
				}
	
				String sign = Md5Util.md5Signature(treeMap, secretKey);
				treeMap.put("sign", sign);
				Iterator<String> iterator = treeMap.keySet().iterator();
				
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				while (iterator.hasNext()) {
					String key = iterator.next();
					params.add(new BasicNameValuePair(key, treeMap.get(key)));
				}
	
				UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
				httpPost.setEntity(uefEntity);
	
				HttpResponse response = httpClient.execute(httpPost);
				HttpEntity httpEntity = response.getEntity();
				InputStream inputStream = httpEntity.getContent();
	
				//获取返回的数据信息
				StringBuffer postResult = new StringBuffer();
				String readLine = null ;
				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
				while ((readLine = reader.readLine()) != null) {
					postResult.append(readLine);
				}
	
				httpClient.getConnectionManager().shutdown();
	
				return postResult.toString();
	
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}


--- 共有 1 条评论 ---
David_Lee_噢,了解! 5年前 回复
顶部