参考文档:https://cloud.tencent.com/document/product/663/83253
首先需要 php 配置支持 openssl
1、批量申请长安链用户签名证书
根据文档请求参数不难发现,生成 csr 文件内容后再经过了 base64 编码的,
/**
* php生成证书请求文件
* @param $organization 组织id 例:orgbeijing.chainmaker-demo
* @param $commonName 用户名 建议和参数CertMark一致 例:user1 ,每次都必须不一样
* @return string
*/
public function getCsr($organization, $commonName){
$key_pair = openssl_pkey_new([
"private_key_type" => OPENSSL_KEYTYPE_EC,
"curve_name" => 'prime256v1'
]);
// 判断密钥是否生成成功
if ($key_pair === false) {
die('Failed to generate encryption key pair.');
}
// 获取私钥
openssl_pkey_export($key_pair, $priKey);
$dn = array(
"countryName" => "CN",
"stateOrProvinceName" => "Beijing",
"localityName" => "Beijing",
"organizationName" => $organization,
"organizationalUnitName" => "client",
"commonName" => $commonName
);
$csr = openssl_csr_new($dn, $priKey, array('digest_alg' => 'sha256'));
$csr_pem = openssl_csr_export($csr, $csr_string);
if ($csr_pem === false) {
die('Failed to generate csr key.');
}
// var_dump($csr_string);
return $csr_string;
}
根据文档,不难发现生成csr文件内容后再经过了base64编码作为参数
//调用生成csr文件内容
$csr_string = $this->getCsr('orgbeijing.chainmaker-demo','user0001');
$SignCsrContent = base64_encode($csr_string);
2、从响应参数SignUserCrtList中计算用户链地址
//引入hash函数的包
kornrunner/keccak
/**
* 从x.509证书转换成 链地址
* @param $certificate x.509证书
* @return string
* @throws \Exception
*/
public function certToAddrStr($certificate){
$certInfo = openssl_x509_read($certificate);
$pub_key = openssl_pkey_get_public($certInfo);
$pub_details = openssl_pkey_get_details($pub_key);
$ecParams = $pub_details['ec'];
//参数x'参数y
$x = unpack("H*", $ecParams['x']);
$y = unpack("H*", $ecParams['y']);
$hash = Keccak::hash(hex2bin($x[1].$y[1]), 256);
return '0x'.substr($hash,24 );
}
先需要base64_decode还原成证书字符串;
// base64解码为证书字符串,再转用户地址
$certificate = base64_decode('LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JS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