PHP 代码示例( Linux 版)¶

解压后,参考 phplinux/v3.4.0.1/文档/PHP版服务器端工具包(Linux版)软件使用手册.pdf

Demo 运行

1.安装对应版本的 PHP

2.安装运行时环境(glibc 库等)

3.修改 PHP 的配置文件 php.ini

修改 php.ini,使 php 允许加载扩展,并将当前扩展添加到其扩展列表中

enable_dl = On

extension=libSADKExtension.so.3.4.0.1

4.在 DemoRSA 目录下替换证书和 cer 文件

pfx 为私钥文件请妥善保管不要泄露给他人

cer 文件为颁发者公钥,用来验证汇付公钥

5.配置 cfcalog.conf cfca 日志文件

6.通过命令行终端运行 Demo 文件

php huifuCFCALinuxDemo.php

Msg PKCS7-attached Sign

为使用 pfx 证书加签

PKCS7-attached-Verify

为验证汇付的签名

cfca_verifyCertificate

为验证证书链合法性

cfca_getCertificateInfo

为获取证书信息(非必要)

php

class HuifuCFCA

{

private $apiUrl = 'https://eacloud.testpnr.com/api/publicRequests'; //企账通商户交易接口,此处使用的是联调环境地址

private $strSignAlg = 'RSA'; //RSA证书类型

private $strPfxPassword = '888888'; //导出时设置的密码

private $strHashAlg = 'SHA-256'; //加签算法

private $strPfxFilePath = './RSA/AS0381.pfx'; //汇付下发的证书,此处换成商户自己的证书 .pfx 格式 加签使用

private $strTrustedCACertFilePath = './RSA/CFCA_ACS_TEST_OCA31.cer|./RSA/CFCA_ACS_TEST_CA.cer'; //汇付下发的.cer证书 ,需要一对证书 解签使用

private $strLogCofigFilePath = './cfcalog.conf'; //CFCA log 目录

public function __construct()

{

$this->getCFCAInitialize(); //CFCA工具初始化

}

/**

* CFCA工具初始化

*/

private function getCFCAInitialize()

{

$nResult = cfca_initialize($this->strLogCofigFilePath);

if (0 != $nResult) {

//记录log

echo new Exception("\n cfca_Initialize error:".$nResult."\n");

}

}

/**

* 调用接口 此处是企账通的接口请求

*

* @return string

*/

public function apiRequest(){

//请求参数,依据商户自己的参数为准

$requestParam['version'] = '10';

$requestParam['cmd_id'] = 'Q01'; //交易订单查询

$requestParam['mer_cust_id'] = '6666000000002619';

$requestParam['user_cust_id'] = '6666000000054387';

$requestParam['order_date'] = '20180918';

$requestParam['order_id'] = '201809189000001';

$requestParam['trans_type'] = '01';

$requestParam['mer_priv'] = '';

//加签

$strSignSourceData = json_encode($requestParam);

$cfcaSign = $this->CFCASignature($strSignSourceData);

//接口请求参数

$param = [

'requestData' => [

'cmd_id' => $requestParam['cmd_id'],

'mer_cust_id' => $requestParam['mer_cust_id'],

'version' => $requestParam['version'],

'check_value' => $cfcaSign,

],

'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8']

];

$requestData = $this->requestData($param);

$checkValue = json_decode($requestData['body'],1)['check_value'];

//验证接口返回的签名数据

$sourceData = $this->getCFCASignSourceData($checkValue);

$SignCertContent = !empty($sourceData['strMsgP7AttachedSignCertContent']) ? $sourceData['strMsgP7AttachedSignCertContent'] : '';

//验证返回数据的CFCA证书有效性

$verifyCertificat = $this->verifyCertificat($SignCertContent);

$signSourceData = '';

if(!empty($sourceData['strMsgP7AttachedSource']) && $verifyCertificat){ //校验证书有效性

$signSourceData = $sourceData['strMsgP7AttachedSource'];

}

return $signSourceData;

}

/**

* CFCA 加签方法

*

* @param $strSignSourceData base64 encode 加签原串

* @return string base64 encode 加签串

*/

private function CFCASignature($strSignSourceData){

$strMsgPKCS7AttachedSignature = '';//加签生成串 ,输出变量,无需传值

try{

//调用加签方法

$nResult = cfca_signData_PKCS7Attached($this->strSignAlg, $strSignSourceData,

$this->strPfxFilePath, $this->strPfxPassword, $this->strHashAlg,$strMsgPKCS7AttachedSignature);

//加签方法异常判断及记录

if (0 != $nResult) {

//记录log

echo new Exception("\n cfca_signData_PKCS7Attached error:".$nResult."\n");

}

}catch (Exception $e){

throw new Exception("\n cfca_verifyCertificate error:".$e."\n");

}

return base64_encode($strMsgPKCS7AttachedSignature);

}

/**

* CFCA 验证签名数据

*

* @param $signature

* @return array

*/

private function getCFCASignSourceData($signature){

$strMsgP7AttachedSignCertContent = ''; //PKCS#7 中的签名证书 输出变量,无需传值

$strMsgP7AttachedSource = ''; //签名原文字符串 输出变量,无需传值

try{

//调用验证签名数据方法

$nResult = cfca_verifyDataSignature_PKCS7Attached($this->strSignAlg, base64_decode($signature),

$strMsgP7AttachedSignCertContent,$strMsgP7AttachedSource);

//验证签名方法异常判断及记录

if (0 != $nResult) {

//记录log

echo new Exception("cfca_verifyDataSignature error:".$nResult);

}

}catch (Exception $e){

//记录log

throw new Exception("cfca_verifyDataSignature_PKCS7Attached error:".$e);

}

return array(

'strMsgP7AttachedSource' => $strMsgP7AttachedSource,

'strMsgP7AttachedSignCertContent' => $strMsgP7AttachedSignCertContent,

);

}

/**

* CFCA 证书有效性验证

*

* @param $strMsgP7AttachedSignCertContent PKCS#7 中的签名证书 base64

* @return int

*/

private function verifyCertificat($strMsgP7AttachedSignCertContent = ''){

$nCertVerifyFlag = '4'; //验证证书链完整性

$strTrustedCACertFilePath = $this->strTrustedCACertFilePath;

$isVerify = false;

try{

//调用验证方法

$nResult = cfca_verifyCertificate($strMsgP7AttachedSignCertContent, $nCertVerifyFlag, $strTrustedCACertFilePath,"");

if (0 == $nResult) { // 0 为验证通过 ,其他验证失败

$isVerify = true;

}else{

//记录log

echo new Exception("cfca_verifyCertificate error:".$nResult);

}

}catch (Exception $e){

//记录log

throw new Exception("cfca_verifyCertificate error:".$e);

}

return $isVerify;

}

/**

* 请求接口返回数据

* @param $param

* @return array

*/

private function requestData($param)

{

try{

// 请求接口所以参数初始化

$data = [

'url' => $this->apiUrl, // 接口 url

'requestData' => $param['requestData'], // 请求接口参数

'headers' =>$param['headers']

];

$res = $this->httpPostRequest($data['url'],$data['headers'],$data['requestData']);

} catch (\Exception $e) {

//记录log

throw new Exception("api requestData error :".$e);

}

return [

'status' => $res['info']['http_code'],

'body' => $res['body']

];

}

/**

* curl post 请求方法

*

* @param string $url

* @param array $header

* @param array $requestData

* @return array

*/

private function httpPostRequest($url = '',$header = array(),$requestData = array()){

$curl = curl_init();

curl_setopt ( $curl, CURLOPT_HTTPHEADER,$header);

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($requestData));

$res = curl_exec($curl);

$info = curl_getinfo($curl);

$error = curl_error($curl);

curl_close($curl);

return [

'body' => $res,

'info' => $info,

'error' => $error,

];

}

/**

*CFCA工具结束

*/

public function __destruct()

{

cfca_uninitialize();

}

}

//调用

$demoObj = new HuifuCFCA();

$data = $demoObj->apiRequest();

print_r('

');

print_r($data);

?>

java软件开发ea介绍_开发说明 — Eacloud 1.0 documentation相关推荐

  1. 开发 自我介绍_对于开发者来说,自我是敌人

    开发 自我介绍 This post was originally published on Medium.I recently finished reading the book "Ego ...

  2. 手机的java软件有哪些功能_手机有哪些软件可以练习JAVA?

    这里介绍一个手机软件-AIDE,相当于手机上的Java集成开发环境,可以直接编辑运行Java程序,除此之外还可以开发简单的安卓应用和手机小游戏,下面我简单介绍一下这个软件的安装和使用: 1.首先,下载 ...

  3. homestead开发php,介绍ThinkPHP开发环境之Homestead

    下面由thinkphp教程栏目给大家介绍ThinkPHP开发环境之Homestead,希望对需要的朋友有所帮助! 如何搭建 ThinkPHP 开发环境 我们知道 Homestead 是 Laravel ...

  4. c++ 开发虚拟摄像头_开发板有了,但我们要怎么玩?

    我们拿到的设备,是一个套件,各个零件单独放置的,那我们需要怎么玩呢? 一起来看看乔版主怎么介绍玩法吧! Wi-Fi IoT 基础目标,是能控制LED灯.点亮LED显示屏: 如果玩得高级一点,还可以通过 ...

  5. java开发工作经历_开发人员在寻找第二份工作时会经历什么

    java开发工作经历 Just a little background before we begin. I have been working as a mobile app developer f ...

  6. JAVA工作总是维护项目_开发维护大型 Java 项目的建议

    原标题:开发维护大型 Java 项目的建议 假设你是正在开发和维护一个包含2000个类并使用了很多框架的Java开发者.你要如何理解这些代码?在一个典型的Java企业项目小组中,大部分能够帮你的高级工 ...

  7. Java中的变量分类_开发简单的Java应用

    第一章 开发简单的Java应用 1.Java语言的技术分类 (1)JavaSE:标准版,开发桌面应用 (2)JavaEE:企业版,Web应用 (3)JavaME:小型版,嵌入式 2.如何开发和运行Ja ...

  8. java netty能做什么_开发:Netty快速入门,一看就懂!

    很早以前就写过关于 Netty 的使用,最近发现还有网友在看之前写的那篇 Netty 文章,个人感觉那时候写的很粗糙,怕影响同行的阅读质量,所以决定重新写一些关于 Netty 的文章,补充以前的不足. ...

  9. 20200928 003.开发环境介绍_交互模式的使用_IDLE介绍和使用

    Python 开发环境 开发环境,英文是 IDE( Integrated Development Environment 集成开发环境 ). 不要纠结于使用哪个开发环境.开发环境本质上就是对 Pyth ...

最新文章

  1. vba 编辑combobox内容_初识Visual Basic编辑器并建立一段简单的代码
  2. pyqt5 输入确认_对PyQt5的输入对话框使用(QInputDialog)详解
  3. 3DSlicer23:Module-Create Loadable
  4. 微信终端跨平台组件 mars 系列(二) - 信令传输超时设计
  5. go执行二进制文件的方法:通过shell脚本来调用二进制文件,直接执行go的二进制文件会存在参数传递问题
  6. 微信小程序php后台支付,微信小程序 支付功能实现PHP实例详解
  7. 【双百解法】剑指 Offer 11. 旋转数组的最小数字
  8. SAP Hybris的Convertor, Populator, Facade和DTO这几个概念是如何协同工作的
  9. hbase伪分布式配置
  10. datagridview取消默认选中_winform datagridview中的 combobox如何选中默认值?
  11. 【linux系统编程】理解冯•诺依曼体系结构
  12. 理解思科IPS系统的traffic flow notifications
  13. POM思想__多个页面时进行的处理
  14. 施耐德somachine4.1注册工具
  15. SpringBoot自动解压Gzip请求
  16. 解决DLL load failed while importing _imaging: 找不到指定的模块。问题
  17. win7万能声卡驱动_我把一台PC的操作系统从win7换成了win10,它真的很棒!
  18. 每日必应壁纸API接口源码
  19. 剑指数据仓库-Hive02
  20. 锁相环(PLL)基本原理

热门文章

  1. extjs grid 整行变颜色_EXTJS根据值Value改变gridpanel单元格背景颜色或者设置整行字体颜色...
  2. python实现接口自动化_python 实现接口自动化1
  3. Nacos 集群搭建_01
  4. “资源添加到Web应用程序[]的缓存中,因为在清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间”
  5. 编写一个函数,该函数能判断一个英文句子str(带空格)中是否含有某个单词w,如“How old are you?”含有“old”。在main函数中输入一个英文句子,再输入一个单词,如果英文句子中含有那
  6. pythonrgb高精度浮点运算类型_python实现RGB字符串,按24位对齐后输出对应Integer行数字...
  7. Qt下Tcp通信的简单使用三
  8. 安装 Visual Studio 插件 Visual Assist - C语言零基础入门教程
  9. Python __name__ == ‘__main__’详细解释-Python零基础入门教程
  10. linux环境下安装多个任意版本的python环境