88 lines
2.6 KiB
Java
88 lines
2.6 KiB
Java
package com.xwad.os.utils;
|
|
|
|
import android.util.Log;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.SortedMap;
|
|
import java.util.TreeMap;
|
|
|
|
public class JxwUtils {
|
|
private static final String TAG = "JxwUtils";
|
|
|
|
static {
|
|
System.loadLibrary("xuewang"); // 加载 libjniutils.so
|
|
}
|
|
|
|
// 声明为Native方法
|
|
public static native String getAppId();
|
|
|
|
public static native String getAppSecret();
|
|
|
|
public static String getSignature(Map<String, String> paramMap) {
|
|
long timeStamp = System.currentTimeMillis();
|
|
//用于获取signature
|
|
SortedMap<String, String> items = new TreeMap<>();
|
|
items.put("nonce", "X6SH3YeRTs");
|
|
items.put("appId", JxwUtils.getAppId());
|
|
items.put("appsecret", JxwUtils.getAppSecret());
|
|
items.put("timestamp", String.valueOf(timeStamp));
|
|
// items.putAll(paramMap);
|
|
|
|
// items.put("其它参数", "其它参数的值");
|
|
String signature = signature(items);
|
|
Log.e(TAG, "userScore: " + signature);
|
|
|
|
|
|
// //请求参数设置
|
|
// Map<String, String> paramMap = new HashMap<>();
|
|
// paramMap.put("nonce", "X6SH3YeRTs");
|
|
// paramMap.put("appId", JxwUtils.getAppId());
|
|
// paramMap.put("signature", signature);
|
|
// paramMap.put("timestamp", String.valueOf(timeStamp));
|
|
//// paramMap.put("其它参数", "其它参数的值");
|
|
|
|
return signature;
|
|
}
|
|
|
|
public static String signature(SortedMap<String, String> items) {
|
|
StringBuilder forSign = new StringBuilder();
|
|
|
|
for (String key : items.keySet()) {
|
|
forSign.append(key).append("=").append(items.get(key)).append("&");
|
|
}
|
|
|
|
forSign.setLength(forSign.length() - 1);
|
|
|
|
return encryptSHA1(forSign.toString());
|
|
}
|
|
|
|
public static String encryptSHA1(String content) {
|
|
try {
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
|
digest.update(content.getBytes(StandardCharsets.UTF_8));
|
|
byte[] messageDigest = digest.digest();
|
|
StringBuilder hexString = new StringBuilder();
|
|
|
|
for (byte b : messageDigest) {
|
|
String shaHex = Integer.toHexString(b & 0xFF);
|
|
|
|
if (shaHex.length() < 2) {
|
|
hexString.append(0);
|
|
}
|
|
|
|
hexString.append(shaHex);
|
|
}
|
|
|
|
return hexString.toString();
|
|
} catch (NoSuchAlgorithmException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|