虎符三方互通场景 签名计算 demo 示例

public class HufuBackendSign {
    private static Logger logger = LoggerFactory.getLogger(HufuBackendSign.class);

    public static String sign(String queryString, String body, String secretKey) {
        HufuBackendSign sign = new HufuBackendSign();
        Map<String, Object> params = sign.getParamsFromUrl(queryString);
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        String joinedParams = sign.joinRequestParams(params, body, secretKey, keys);
        String signMethod = (String) params.get("sign_method");
        if (!"md5".equalsIgnoreCase(signMethod)) {
            return null;
        } else {
            byte[] abstractMesaage = sign.digest(joinedParams);
            String md5 = sign.byte2Hex(abstractMesaage);
            return md5;
        }
    }

    public static String sign(Map<String, Object> urlParams, String body, String secretKey) {
        HufuBackendSign sign = new HufuBackendSign();
        Map<String, Object> params = sign.getParamsFromUrl(urlParams);
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        String joinedParams = sign.joinRequestParams(params, body, secretKey, keys);
        String signMethod = params.get("sign_method").toString();
        if (!"md5".equalsIgnoreCase(signMethod)) {
            return null;
        } else {
            byte[] abstractMesaage = sign.digest(joinedParams);
            String md5 = sign.byte2Hex(abstractMesaage);
            logger.debug("签名结果:{}", md5);
            return md5;
        }
    }

    private HashMap getParamsFromUrl(String queryString) {
        HashMap requestParams = new HashMap();
        try {
            String fullUrl = URLDecoder.decode(queryString, "UTF-8");
            String[] paramArray = fullUrl.split("&");
            String[] var9 = paramArray;
            int var8 = paramArray.length;

            for (int var7 = 0; var7 < var8; ++var7) {
                String param = var9[var7];
                String[] params = param.split("=");
                if (params.length == 2) {
                    requestParams.put(params[0], params[1]);
                }
            }
            return requestParams;
        } catch (UnsupportedEncodingException var11) {
            return null;
        }
    }

    private Map<String, Object> getParamsFromUrl(Map<String, Object> urlParams) {
        try {
            for (Map.Entry entry : urlParams.entrySet()) {
                entry.setValue(URLDecoder.decode(entry.getValue().toString(), "UTF-8"));
            }
            return urlParams;
        } catch (UnsupportedEncodingException var11) {
            return null;
        }
    }

    private String byte2Hex(byte[] bytes) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        int j = bytes.length;
        char[] str = new char[j * 2];
        int k = 0;
        byte[] var9 = bytes;
        int var8 = bytes.length;

        for (int var7 = 0; var7 < var8; ++var7) {
            byte byte0 = var9[var7];
            str[k++] = hexDigits[byte0 >>> 4 & 15];
            str[k++] = hexDigits[byte0 & 15];
        }

        return new String(str);
    }

    private byte[] digest(String message) {
        try {
            MessageDigest md5Instance = MessageDigest.getInstance("MD5");
            md5Instance.update(message.getBytes("UTF-8"));
            return md5Instance.digest();
        } catch (UnsupportedEncodingException var3) {
            return null;
        } catch (NoSuchAlgorithmException var4) {
            return null;
        }
    }

    private String joinRequestParams(Map<String, Object> params, String body, String secretKey, String[] sortedKes) {
        StringBuilder sb = new StringBuilder(secretKey);
        String[] var9 = sortedKes;
        int var8 = sortedKes.length;

        for (int var7 = 0; var7 < var8; ++var7) {
            String key = var9[var7];
            if (!"sign".equals(key)) {
                String value = (String) params.get(key);
                if (this.isNotEmpty(key) && this.isNotEmpty(value)) {
                    sb.append(key).append(value);
                }
            }
        }

        sb.append(body);
        sb.append(secretKey);
        logger.debug("后端签名字符串:{}", sb.toString());
        return sb.toString();
    }

    private boolean isNotEmpty(String s) {
        return s != null && !"".equals(s);
    }
}

results matching ""

    No results matching ""

    results matching ""

      No results matching ""