part of utils;

class SignTool {
  static String createSign(Map<String, dynamic> params) {
    Map<String, dynamic> tempParams = keySort(params);
    List<String> tempArr = [];
    for (String key in tempParams.keys){
      tempArr.add("$key=${tempParams[key]}");
    }
    String tempStr = tempArr.join("&");
    String finalString = tempStr + AppConfig.appEncodeKey;
    String finalEncodeStr = EncryptUtil.encodeMd5(EncryptUtil.encodeMd5(finalString));
    return finalEncodeStr;
  }


  ///首字母排序得到排序后新的map
  static Map<String,dynamic> keySort(Map<String,dynamic> params) {
    Map<String, dynamic> tempParam = <String,dynamic>{};
    List<String> oldKeys = params.keys.toList();
    if(oldKeys.isEmpty) return tempParam;
    oldKeys.sort((a,b){
      List<int> al = a.codeUnits;
      List<int> bl = b.codeUnits;
      for(int i = 0;i< al.length;i++){
        if(bl.length <= i) return 1;
        if(al[i]>bl[i]){
          return 1;
        }
        else if (al[i] < bl[i]){
          return -1;
        }
      }
      return 0;
    });
    for (int i = 0; i< oldKeys.length;i++){
      tempParam[oldKeys[i]]= params[oldKeys[i]];
    }
    return tempParam;
  }
}