eleme.openapi.sdk.utils.SignatureUtil Maven / Gradle / Ivy
The newest version!
package eleme.openapi.sdk.utils;
import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;
public class SignatureUtil {
public static String generateSignature(String appKey, String secret, long timestamp, String action, String token, Map parameters) {
final Map sorted = new TreeMap();
for (Map.Entry entry : parameters.entrySet()) {
sorted.put(entry.getKey(), entry.getValue());
}
sorted.put("app_key", appKey);
sorted.put("timestamp", timestamp);
StringBuffer string = new StringBuffer();
for (Map.Entry entry : sorted.entrySet()) {
if (entry.getValue()==null){
continue;
}
string.append(entry.getKey()).append("=").append(JacksonUtils.obj2json(entry.getValue()));
}
String splice = String.format("%s%s%s%s", action, token, string, secret);
//System.out.println(splice + "\n\n");
String calculatedSignature = md5(splice);
return calculatedSignature.toUpperCase();
}
public static String md5(String str) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
md.update(str.getBytes("UTF-8"));
} catch (Exception e) {
}
byte byteData[] = md.digest();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < byteData.length; i++)
buffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
return buffer.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy