快手签名算法来源于网络,本站只负责整理,从快手签名算法上看,快手所有的接口基本都用到了一个参数sig(数据签名)。本篇文章主要是用java语言写出这个算法。
下面是java代码:
public class SingatureUtil { private static final String FANS_SALT = "382700b563f4"; public static String genSignature(Map<String,String> params,String salt) { if(params == null){ return null; } String sign = ""; StringBuffer sb = new StringBuffer(); try { // 1. 字典升序排序 SortedMap<String,String> sortedMap = new TreeMap<>(params); // 2. 拼按URL键值对 Set<String> keySet = sortedMap.keySet(); for(String key : keySet){ //sign不参与算法 if(key.equals("sig") || key.equals("__NStokensig")){ continue; } String value = sortedMap.get(key); sb.append(key + "=" + URLDecoder.decode(value,"UTF-8")); } String uriString = sb.toString(); uriString = uriString + salt; System.out.println("My String: \n" + uriString); // 3. MD5运算得到请求签名 sign = MD5Util.md5(uriString); System.out.println("My Sign:\n" +sign); } catch (Exception e) { e.printStackTrace(); } return sign; } public static Map<String,String> getMapFromStr(String str){ if(StringUtils.isEmpty(str)){ return null; } String[] arr = str.split("\\&"); Map<String,String> map = new HashMap<>(); for(String item : arr){ String[] itemArr = item.split("=",2); map.put(itemArr[0],itemArr[1]); } return map; } public static void main(String[] args) throws UnsupportedEncodingException { String srcStr = "app=0&lon=104.073269&did_gt=1551777466213&c=XIAOMI&sys=ANDROID_4.4.4&isp=&mod=Xiaomi%28MI%203%29&did=ANDROID_b07d34ee8ff226b0&hotfix_ver=&ver=6.1&net=WIFI&country_code=cn&iuid=&appver=6.1.2.8197&max_memory=192&oc=XIAOMI&ftt=&kpn=KUAISHOU&ud=1273257807&language=zh-cn&kpf=ANDROID_PHONE&lat=30.537794&user=74476707&token=6f8b8954c34e4462a1c0117ac5a5af21-1273257807&os=android&client_key=3c2cd3f3&sig=8ab207f1762b17b47d1ca0cc26ce6576&__NStokensig=334b7f77f9fec536c1dce00467f8cf79bed4f66cd8a24ffc205b3e1a151ab1e7"; genSignature(getMapFromStr(srcStr),FANS_SALT); } } 运行结果如下: My String: app=0appver=6.1.2.8197c=XIAOMIclient_key=3c2cd3f3country_code=cndid=ANDROID_b07d34ee8ff226b0did_gt=1551777466213ftt=hotfix_ver=isp=iuid=kpf=ANDROID_PHONEkpn=KUAISHOUlanguage=zh-cnlat=30.537794lon=104.073269max_memory=192mod=Xiaomi(MI 3)net=WIFIoc=XIAOMIos=androidsys=ANDROID_4.4.4token=6f8b8954c34e4462a1c0117ac5a5af21-1273257807ud=1273257807user=74476707ver=6.1382700b563f4 My Sign: 8ab207f1762b17b47d1ca0cc26ce6576
代码中出现的【382700b563f4】这个salt值,在native层libcore.so这个so里面,具体分析可得。