java type causes inconsistent md5 signature results

<div class="iteye-blog-content-contain" style="font-size: 14px;">
<p> I recently found a problem when connecting to the interface of a third-party company. If md5 encryption signature is used, the unit test When using the same parameter values ​​as the test environment to perform md5 signature, the results are found to be inconsistent. After some tossing and asking others, it is found that the type is caused by the type, so go directly to the code</p>
<pre name="code" class="java" >public class SignExample {
private static final String key = "123456";
public static void main(String[] args) {
/*Map<String, Object> map = new HashMap<>();
map.put("abc ", 234);
map.put("dfe", "daw");
map.put("bbd", System.currentTimeMillis());//2598128947B95E1DF910AB7F41DAB779 36D6EFC1DF9E4F864862000A0230806F
generateSign(map, key);*/

Map<String, String> map = new HashMap<>();
map.put("abc", "234");
map.put("dfe", "daw");
map.put("bbd", String.valueOf(System.currentTimeMillis()));
generateSign(map, key);

}


/**
     * 生成签名
     *  @param params
     *  @param  key
     *  @return md5加密签名
     */
    private static Map<String, String> generateSign(Map<String, String> params, String key) {
        String param = paramFilter(params);
        Map<String, String> signMap = new HashMap<>(2);
        String sign = DigestUtils.md5Hex((param + "&key=" + key).getBytes()).toUpperCase();
        signMap.put("urlSuffix", param);
        signMap.put("sign", sign);
        System.out.println(sign);
        return signMap;
    }

    /**
     * 过滤值不为空和参数不是sign sign_type的参数
     * @param params
     * @return
     */
    private static String paramFilter(Map<String, String> params) {
        if (null == params || params.isEmpty()) {
            return null;
        }
        List<String> paramList = new ArrayList<>();
        params.entrySet().forEach(param -> {
            if (StringUtils.isNotEmpty(param.getValue().toString()) && !"sign".equalsIgnoreCase(param.getKey()) && !"sign_type".equalsIgnoreCase(param.getKey())) {
                paramList.add(param.getKey() + "=" + param.getValue());
            }
        });
        Collections.sort(paramList);
        return paramList.stream().collect(Collectors.joining("&"));
    }
}</pre>
<p> 上面的md5加密直接使用的commons-codec</p>
<p><dependencies></p>
<p>  <dependency></p>
<p>        <groupId>commons-codec</groupId></p>
<p>        <artifactId>commons-codec</artifactId></p>
<p>        <version>1.10</version></p>
<p>      </dependency></p>
<p>      <dependency></p>
<p>          <groupId>org.apache.commons</groupId></p>
<p>          <artifactId>commons-lang3</artifactId></p>
<p> <version>3.4</version></p>
<p> </dependency></p>
<p> </dependencies></p>
<p> </p>
<p > I annotated a part of the main function above. After you change the type, you will find that the results are inconsistent. </p>
<p> </p>
<p>Welcome to join the qq learning exchange group 513650703 to communicate and learn together</p>
<p> </p>
<p> </p>
<p> </p>
< /div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326084789&siteId=291194637