33
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package cn.shangyu.gdxzExpert.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.Base64;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Sign {
|
||||
// 固定
|
||||
public static final String PARTNERID = "medicoolgandan2017";
|
||||
|
||||
public static String getSign(Context context, String method,
|
||||
String nonce_str, LinkedHashMap<String, Object> parameters)
|
||||
throws Exception {
|
||||
String sign;
|
||||
// JSONObject jsonObject = new JSONObject();
|
||||
// String j_parameters = "";
|
||||
// //对parameters进行json_encode处理
|
||||
// try {
|
||||
// if (parameters != null && !parameters.isEmpty()) {
|
||||
// for (Map.Entry<String, Object> entry : parameters.entrySet()) {
|
||||
// jsonObject.put(entry.getKey(), entry.getValue());
|
||||
// }
|
||||
// j_parameters = jsonObject.toString();
|
||||
// }
|
||||
// } catch (JSONException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
Gson gson = new Gson();
|
||||
String j_parameters = gson.toJson(parameters);
|
||||
// 拼接参数method=$method&nonce_str=$nonce_str¶meters=$parameters&partnerid=$partnerid
|
||||
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer.append("method=").append(method + "&")
|
||||
.append("nonce_str=").append(nonce_str + "&")
|
||||
.append("parameters=").append(j_parameters + "&")
|
||||
.append("partnerid=" + PARTNERID);
|
||||
System.out.println("stringBuffer=== " + stringBuffer);
|
||||
// RSA加密
|
||||
byte[] bytes = RsaUtils.encryptData(stringBuffer.toString().getBytes(),
|
||||
context);
|
||||
|
||||
// base64_encodec
|
||||
sign = Base64.encodeToString(bytes, Base64.DEFAULT);
|
||||
|
||||
// urlencode
|
||||
sign = Uri.encode(sign);
|
||||
|
||||
return sign;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user