Java

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.TreeMap;

public class SignGenerator {
    public static String generateSign(Map<String, String> params, String secretKey) throws NoSuchAlgorithmException {
        long timestamp = System.currentTimeMillis() / 1000L;
        Map<String, String> sortedParams = new TreeMap<>(params);
        sortedParams.put("timestamp", Long.toString(timestamp));
        StringBuilder signStr = new StringBuilder();
        for (Map.Entry<String, String> entry : sortedParams.entrySet()) {
            signStr.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        signStr.append(secretKey);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(signStr.toString().getBytes());
        StringBuilder sign = new StringBuilder();
        for (byte b : digest) {
            sign.append(String.format("%02x", b & 0xff));
        }
        return sign.toString();
    }
}

In this implementation, the generateSign function takes a Map of parameters and a String secret key as input. It generates a timestamp using System.currentTimeMillis(), sorts the parameters using a TreeMap, and adds the timestamp to the sorted parameters. It then concatenates the sorted parameters and the secret key into a string, computes the MD5 hash of the resulting string, and returns the hash as a string.

Last updated