/**?
*?SHA1?安全加密算法? *?@param?maps?參數key-value?map集合? *?@return? *?@throws?DigestException? */?public?static?String?SHA1(Map<String,Object>?maps)?throws?DigestException?{?
//獲取信息摘要?-?參數字典排序後字符串?
String?decrypt?=?getOrderByLexicographic(maps);?
try?{?
//指定sha1算法?
MessageDigest?digest?=?MessageDigest.getInstance("SHA-1");?
digest.update(decrypt.getBytes());?
//獲取字節數組?
byte?messageDigest[]?=?digest.digest();?
//?Create?Hex?String?
StringBuffer?hexString?=?new?StringBuffer();?
//?字節數組轉換為?十六進制?數?
for?(int?i?=?0;?i?<?messageDigest.length;?i++)?{?
String?shaHex?=?Integer.toHexString(messageDigest[i]?&?0xFF);?
if?(shaHex.length()?<?2)?{?
hexString.append(0);?
}?
hexString.append(shaHex);?
}?
return?hexString.toString().toUpperCase();?
}?catch?(NoSuchAlgorithmException?e)?{?
e.printStackTrace();?
throw?new?DigestException("簽名錯誤!");?
}?
}?
/**?
*?獲取參數的字典排序? *?@param?maps?參數key-value?map集合? *?@return?String?排序後的字符串? */?private?static?String?getOrderByLexicographic(Map<String,Object>?maps){?
return?splitParams(lexicographicOrder(getParamsName(maps)),maps);?
}?
/**?
*?獲取參數名稱?key? *?@param?maps?參數key-value?map集合? *?@return? */?private?static?List<String>?getParamsName(Map<String,Object>?maps){?
List<String>?paramNames?=?new?ArrayList<String>();?
for(Map.Entry<String,Object>?entry?:?maps.entrySet()){?
paramNames.add(entry.getKey());?
}?
return?paramNames;?
}?
/**?
*?參數名稱按字典排序? *?@param?paramNames?參數名稱List集合? *?@return?排序後的參數名稱List集合? */?private?static?List<String>?lexicographicOrder(List<String>?paramNames){?
Collections.sort(paramNames);?
return?paramNames;?
}?
/**?
*?拼接排序好的參數名稱和參數值? *?@param?paramNames?排序後的參數名稱集合? *?@param?maps?參數key-value?map集合? *?@return?String?拼接後的字符串? */?private?static?String?splitParams(List<String>?paramNames,Map<String,Object>?maps){?
StringBuilder?paramStr?=?new?StringBuilder();?
for(String?paramName?:?paramNames){?
paramStr.append(paramName);?
for(Map.Entry<String,Object>?entry?:?maps.entrySet()){?
if(paramName.equals(entry.getKey())){?
paramStr.append(String.valueOf(entry.getValue()));?
}?
}?
}?
return?paramStr.toString();?
}