通常生成签名的时候都会排序,看源码时偶尔发现了比较优雅的写法。
public static String generateSign(final String signKey, final Map<String, String> params) {
List<String> storedKeys = Arrays.stream(params.keySet()
.toArray(new String[]{}))
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
final String sign = storedKeys.stream()
.filter(key -> !Objects.equals(key, Constants.SIGN))
.map(key -> String.join("", key, params.get(key)))
.collect(Collectors.joining()).trim()
.concat(signKey);
return DigestUtils.md5DigestAsHex(sign.getBytes()).toUpperCase();
}