テンセントモバイル分析MTA HTML 5統計sig生成アルゴリズム(JAVA版)
5020 ワード
このsig生成アルゴリズムには疑問な方法があります.接続しないことです.
// “ ”
public Map paramMap(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
String endDate = sdf.format(today);//
Calendar theCa = Calendar.getInstance();
theCa.setTime(today);
theCa.add(theCa.DATE, -30);// 30 ,30
Date start = theCa.getTime();
String startDate = sdf.format(start);//
Map map = new LinkedHashMap<>();// ( LinkedHashMap )
map.put("app_id",APP_ID);
map.put("end_date",endDate);
map.put("idx","pv,uv");
map.put("start_date",startDate);
Map tmap = MapUtil.order(map);// map , LinkedHashMap。
String str = SECRET_KEY+MapUtil.mapJoin(tmap, false, false).replaceAll("&","");// map , &, &, &
map.put("sign",DigestUtils.md5Hex(str));//DigestUtils.md5Hex(str) md5 sign
return map;
}
MapUtil MapUtil.order(Map) hashmap MapUtil.mapJoin(Map,boolean,boolean) map
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class MapUtil {
private static Logger logger = LoggerFactory.getLogger(MapUtil.class);
/**
* Map key
* @param map map
* @return map
*/
public static Map order(Map map){
HashMap tempMap = new LinkedHashMap();
List> infoIds = new ArrayList>( map.entrySet());
Collections.sort(infoIds, new Comparator>() {
public int compare(Map.Entry o1,Map.Entry o2) {
return (o1.getKey()).toString().compareTo(o2.getKey());
}
});
for (int i = 0; i < infoIds.size(); i++) {
Map.Entry item = infoIds.get(i);
tempMap.put(item.getKey(), item.getValue());
}
return tempMap;
}
/**
* map
* @param object object
* @param ignore ignore
* @return map
*/
public static Map objectToMap(Object object,String... ignore){
Map tempMap = new LinkedHashMap();
for(Field f : getAllFields(object.getClass())){
if(!f.isAccessible()){
f.setAccessible(true);
}
boolean ig = false;
if(ignore!=null&&ignore.length>0){
for(String i : ignore){
if(i.equals(f.getName())){
ig = true;
break;
}
}
}
if(ig){
continue;
}else{
Object o = null;
try {
o = f.get(object);
} catch (IllegalArgumentException e) {
logger.error("", e);
} catch (IllegalAccessException e) {
logger.error("", e);
}
tempMap.put(f.getName(), o==null?"":o.toString());
}
}
return tempMap;
}
/**
* url
* @param map map
* @param keyLower keyLower
* @param valueUrlencode valueUrlencode
* @return string
*/
public static String mapJoin(Map map,boolean keyLower,boolean valueUrlencode){
StringBuilder stringBuilder = new StringBuilder();
for(String key :map.keySet()){
if(map.get(key)!=null&&!"".equals(map.get(key))){
try {
String temp = (key.endsWith("_")&&key.length()>1)?key.substring(0,key.length()-1):key;
stringBuilder.append(keyLower?temp.toLowerCase():temp)
.append("=")
.append(valueUrlencode?URLEncoder.encode(map.get(key),"utf-8").replace("+", "%20"):map.get(key))
.append("&");
} catch (UnsupportedEncodingException e) {
logger.error("", e);
}
}
}
if(stringBuilder.length()>0){
stringBuilder.deleteCharAt(stringBuilder.length()-1);
}
return stringBuilder.toString();
}
/**
* Fields, field
* @param clazz clazz
* @return list
*/
private static List getAllFields(Class> clazz){
if(!clazz.equals(Object.class)){
List fields = new ArrayList(Arrays.asList(clazz.getDeclaredFields()));
List fields2 = getAllFields(clazz.getSuperclass());
if(fields2!=null){
fields.addAll(fields2);
}
return fields;
}else{
return null;
}
}
}