如何将下面这段java代码的生成签名部分用PHP重写?
package com.alipay.mychain.taas.sdk.test.tschainsdktest.demo;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
/**
* 简单测试用例
*
* @author minglang.fzy
* @date 2021/3/31 4:49 下午
**/
public class SimpleTest {
/**
* 溯源账户标识
*/
private static final String APP_KEY = "${从商户控制台获取账户标识}";
/**
* 秘钥
*/
private static final String TRACE_APP_PRIVITE_KEY = "${从商户控制台生成秘钥}";
/**
* 访问地址(生产)
*/
private static final String TAAS_URL = "https://tschain.cloud.alipay.com";
/**
* SDK最新版本
*/
public static final String LATEST_VERSION = "V_1_6_5";
@Test
public static void main(String[] args) throws Exception {
// 请求方法uri
String method = "/trace/merchant/list";
// 请求客户端构造
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.readTimeout(1000, TimeUnit.MILLISECONDS)
.connectTimeout(1000, TimeUnit.MILLISECONDS)
.writeTimeout(1000, TimeUnit.MILLISECONDS)
.connectionPool(new ConnectionPool(5, 10000L, TimeUnit.MILLISECONDS))
.build();
// 请求参数
JSONObject queryEntity = new JSONObject();
queryEntity.put("isSelf", false);
queryEntity.put("name", null);
queryEntity.put("merchantId", null);
queryEntity.put("authStatus", null);
JSONObject pageEntity = new JSONObject();
pageEntity.put("num", 1);
pageEntity.put("size", 10);
pageEntity.put("data", queryEntity);
JSONObject commonEntity = new JSONObject();
commonEntity.put("data", pageEntity);
commonEntity.put("appId", APP_KEY);
commonEntity.put("version", LATEST_VERSION);
commonEntity.put("timestamp", System.currentTimeMillis());
// 请求信息序列化为json字符串
String body = commonEntity.toJSONString();
// 对请求body进行签名
String signData = Base64.getEncoder().encodeToString(sign(body.getBytes(Charset.forName("UTF-8")), TRACE_APP_PRIVITE_KEY));
// 请求发送
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body);
Request request = new Request.Builder()
.url(TAAS_URL + method)
.post(requestBody)
.addHeader("api-version", "signature1.0")
.addHeader("sign-data", signData)
.build();
// 返回信息解析
Response response = okHttpClient.newCall(request).execute();
String responseBody = response.body().string();
System.out.println(responseBody);
}
/**
* 签名
*
* @param: plain,明文
* @param: pkcs8 私钥
*/
public static byte[] sign(byte[] plain, String priKey) {
try {
PrivateKey privateKey = loadPriKey(priKey);
Signature signature = Signature.getInstance("Sha256WithRSA");
signature.initSign(privateKey);
signature.update(plain);
return signature.sign();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
/**
* 加载私钥数据
*
* @param: pubKey,私钥文本
*/
public static PrivateKey loadPriKey(String priKey) {
try {
//1.读取私钥文件,获取私钥数据
byte[] bytesPrivateBase64 = readPriKey(priKey);
//2.对读取回来的数据进行Base64解码
byte[] bytesPrivate = Base64.getDecoder().decode(bytesPrivateBase64);
//3.把解码后的数据,重新封装成一个PrivateKey对象
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytesPrivate);
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
private static byte[] readPriKey(String key) {
try {
BufferedReader bufferedReader = new BufferedReader(new StringReader(key));
String str = null;
StringBuilder stringBuilder = new StringBuilder();
while ((str = bufferedReader.readLine()) != null) {
if (str.contains("---")) {
continue;
}
stringBuilder.append(str);
}
return stringBuilder.toString().getBytes();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
}
现在在用的方法不行和java生成的不一样(同样的json串),如果回答和下面的方法一样就不用回答了
:
//生成签名
private function generateSign($request){
$content = json_encode($request);
$privateKey = file_get_contents($this->sslKeyPath);
//var_dump($privateKey);
$key = openssl_get_privatekey($privateKey);
openssl_sign($content, $signature, $key, OPENSSL_ALGO_SHA256);
openssl_free_key($key);
$sign = base64_encode($signature);
return $sign;
}
参考文档:https://antchain.antgroup.com/docs/11/256485#h3-url-3
注:上述地址有java的sdk源码可供参考,REST json 串可以是:
{"data":{"name":"pruduct-liangfang-001","remark":"this is a test data"},"appId":"2088002286496263","version":"V_3_6_1","timestamp":1666162726318}
timestamp:可以写固定值:1666162726318
地址是请求地址:https://tschain.cloud.alipay.com/trace/merchant/register
私钥文本是:
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCC2hTaINLJoyPK
l89R7g5wVJqJVwwEDEXdQeSgFDCn6DA/SvNTpSp6yw0Dhss9xARBThhPMxUjvwZs
3VuUIZsXu3fFmufVSG7MtPewcRnB0na0fad/CspOOH4LL8cZjcv94sQ2+agZddXi
jqt5S9QRm/6ZFdVEOz4NyPcozojjvzVQkberaah4XsgPaAVuBIMXOKMcB+zxjOuk
5NbUb4b6k4f0D2vShUOG2DmHeMfgXs1gUXgO+5eT4UW7KusU7hu1vb07j/64VIiI
VQO41EeEqdF/CBmtkhKNp8tpaOh1x9SMatAzWEeNoa9ztQ6jIcXAukZi0hAa3Hg1
5FFmTFh1AgMBAAECggEASx3SVp39I4+pnkWj2E6mr0mCJVF5LmG1QZzwGA0/GUSr
XcozAVORnUgvQlUBG4YJQ0wHnH+gpRKmJ5yzU8gu9mLCEQgqCkA4iv9gKUPkG2Ys
itSPcNjK5jh78wQMcdj6v9aL+Vs8atFAZXytZ0ZmiW1ZrJvh//daWiOB70URRFvE
kH8fND1tynVA1m8pL+zM21o1zjJWB4y7IkziMFyfdBaqssRXDffOOmGal+b0fD5t
GkcKANMvZw4+UtolEx50/0oWIACiR/qZGmA80Ph4D2nqEQg7aRRgq6TNllICPEMm
sF0ZkfKBnZWoVwTRFP17nITy/psQ0D+o/5t6S11DgQKBgQD86NJXu32lFYsHuvID
8kY2t5ig3l15WcnKBZzjFL3Ruc2kOnxJ1FncNnfVT+uL0kVaBsKkHDQ1Ufgtq5X7
0xgkixR9WR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