hrjy 2 лет назад
Родитель
Сommit
0aabf8d87e

+ 0 - 342
app/api/common.php

@@ -1,343 +1 @@
 <?php
-function string2hex($string){
-    $hex = '';
-    for($i = 0;$i < strlen($string); $i++){
-        $ch = dechex(ord($string[$i]));
-        if(strlen($ch)==1){
-            $ch = "0".$ch;
-        }
-        $hex .=$ch;
-    }
-    return $hex;
-}
-/*
-function hex2string($hex){
-	//echo "$hex \n";
-	$string = '';
-	for($i = 0;$i < strlen($hex);$i++){
-		$string .= chr(hexdec($hex[$i].$hex[$i++]));
-	}
-	return $string;
-}
-*/
-if(!extension_loaded('phpcrypto')) {
-    dl('phpcrypto.so');
-}
-
-$module = 'phpcrypto';
-$functions = get_extension_funcs($module);
-echo "Functions available in the test extension:\n";
-foreach($functions as $func) {
-    echo $func."\n";
-}
-echo "\n";
-
-//先加载动态库,否则无法调用接口
-function test_php_crypto_init(){
-    $php_func = 'php_crypto_init';
-    echo "--------$php_func--------\n";
-    $path = "/www/server/php/73/libcryptAPIsm_lnx64.so";//国密动态库路径
-    $recode = $php_func($path);
-    echo "recode:$recode \n";//如果非零,则加载失败
-}
-
-//test php_SM4Crypt1
-function test_php_SM4Crypt1(){
-    $php_func = 'php_SM4Crypt1';
-    echo "--------$php_func--------\n";
-    $data = "this is a test! 这是个测试!";
-    $key = "1234567890123456";//如果是hex编码,务必解码
-    $recode = $php_func(0,$data,$redata,$key);
-    echo "recode:$recode \n";
-    echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $recode = $php_func(1,$redata,$dedata,$key);
-    echo "recode:$recode \n";
-    echo "plain: ".$dedata." \n";
-}
-
-//test SM4Crypt2
-function test_php_SM4Crypt2(){
-    $php_func = 'php_SM4Crypt2';
-    echo "--------$php_func--------\n";
-    $data = "this is a test! 这是个测试!";
-    $key = "1234567890123456";//如果是hex编码,务必解码
-    $recode = $php_func(0,$data,$redata,$key);
-    echo "recode:$recode \n";
-    echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $recode = $php_func(1,$redata,$dedata,$key);
-    echo "recode:$recode \n";
-    echo "plain: ".$dedata." \n";
-}
-
-//test php_CryptFile
-function test_php_CryptFile(){
-    $php_func = 'php_CryptFile';
-    echo "--------$php_func--------\n";
-    $flag = 2;
-    $infilename = "/home/essc50mysql/mf/src_crypt/test_cryptfile";
-    $encodefilename = "/home/essc50mysql/mf/src_crypt/test_encode_cryptfile";
-    $format = 1;
-    $key = "1234567890123456";//如果是hex编码,务必解码
-    $recode = $php_func($flag, $key, $infilename,$encodefilename,$format);
-    echo "recode:$recode \n";
-    $flag = 3;
-    $decodefilename = "/home/essc50mysql/mf/src_crypt/test_decode_cryptfile";
-    $recode = $php_func($flag, $key, $encodefilename,$decodefilename,$format);
-    echo "recode:$recode \n";
-}
-
-//test php_CryptKey
-function test_php_CryptKey(){
-    $php_func = 'php_CryptKey';
-    echo "--------$php_func--------\n";
-    $flag = 0;
-    $plainkey = "1234567890123456";
-    $recode = $php_func($flag, $plainkey,$cipherkey);
-    echo "recode:$recode \n";
-    echo "cipherkey: ".string2hex($cipherkey)." len:".strlen($cipherkey)." \n";
-    $flag = 2;
-    $recode = $php_func($flag, NULL,$genkey);
-    echo "recode:$recode \n";
-    echo "genkey: ".string2hex($genkey)." len:".strlen($genkey)." \n";
-}
-
-//test php_SM3Crypt
-function test_php_SM3Crypt(){
-    $php_func = 'php_SM3Crypt';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!";
-    $recode = $php_func($data,$sm3hash);
-    echo "recode:$recode \n";
-    echo "sm3hash=================: ".string2hex($sm3hash)." len:".strlen($sm3hash)."============= \n";
-}
-
-//test php_MACCrypt
-function test_php_MACCrypt(){
-    $php_func = 'php_MACCrypt';
-    echo "--------$php_func--------\n";
-    $flag = 0;
-    $data = "this is test 这是测试!this is test 这是测试!";
-    $key = "1234567890123456";
-    $recode = $php_func($flag,$data, $key,$redata);
-    echo "recode:$recode \n";
-    echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $flag = 1;
-    $recode = $php_func($flag,$data, $key,$redata);
-    echo "recode:$recode \n";
-    echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-}
-
-//test php_CryptLmkMac
-function test_php_CryptLmkMac(){
-    $php_func = 'php_CryptLmkMac';
-    echo "--------$php_func--------\n";
-    $recode = $php_func($data,$version, $pubkey);
-    echo "recode:$recode \n";
-    echo "keymac: ".string2hex($data)." version:".$version." pubkey".string2hex($pubkey)." len:".strlen($pubkey)." \n";
-}
-
-//test php_SM2Genkey
-function test_php_SM2Genkey(){
-    $php_func = 'php_SM2Genkey';
-    //echo "--------$php_func--------\n";
-    $recode = $php_func($privkey, $pubkey);
-    //echo "recode:$recode \n";
-    echo "privkey: ".string2hex($privkey)." len:".strlen($privkey)." pubkey: ".string2hex($pubkey)." len:".strlen($pubkey)." \n";
-
-    echo "sm2密钥对:"+array($privkey, $pubkey);
-
-    return array($privkey, $pubkey);
-}
-
-
-//test php_SM2Sign
-function test_php_SM2SignAndSM2Verify(){
-    $php_func = 'php_SM2Sign';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";//数据有中文则需要注意字符编码的问题
-    $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
-    //签名
-    $recode = $php_func($data, $redata,$sm2keys[0],$sm2keys[1]);
-    echo "recode:$recode \n";
-    echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
-    //php_SM2Sign接口返回的签名值是RS格式,以下示例转DER编码
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(202,$redata, $der);
-    echo "recode:$recode \n";
-    echo "signValue DER: ".string2hex($der)." len:".strlen($der)." \n";
-
-    //验签
-    $php_func = 'php_SM2Verify';
-    $recode = $php_func($data, $redata,$sm2keys[1]);
-    echo "recode:$recode \n";
-
-    //php_SM2Verify 接收的签名值是RS格式,若对方给的签名值是DER格式
-    //则通过如下方式转换,然后再验签
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(201,$der, $rs);
-    echo "recode:$recode \n";
-    echo "signValue RS: ".string2hex($rs)." len:".strlen($rs)." \n";
-}
-
-//test php_SM2SignHash php_SM2VerifyHash
-function test_php_SM2SignHashAndSM2VerifyHash(){
-    $php_func = 'php_SM2SignHash';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-    $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
-
-    $php_func = 'php_SM3Crypt';
-    $recode = $php_func($data,$sm3hash);
-    echo "recode:$recode \n";
-    echo "sm3hash: ".string2hex($sm3hash)." len:".strlen($sm3hash)." \n";
-
-    $php_func = 'php_SM2SignHash';
-    $recode = $php_func($sm3hash, $redata,$sm2keys[0]);
-    echo "recode:$recode \n";
-    echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $php_func = 'php_SM2VerifyHash';
-    $recode = $php_func($sm3hash, $redata,$sm2keys[1]);
-    echo "recode:$recode \n";
-}
-
-//test php_SM2Encrypt php_SM2Decrypt
-function test_php_SM2EncryptAndSM2Decrypt(){
-    $php_func = 'php_SM2Encrypt';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-    $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
-
-    //加密
-    $recode = $php_func($data, $cipher,$sm2keys[1]);
-    echo "recode:$recode \n";
-    echo "cipher: ".string2hex($cipher)." len:".strlen($cipher)." \n";
-    //php_SM2Encrypt接口返回的密文值是c1c3c2格式,以下示例转DER编码
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(102,$cipher, $der);
-    echo "recode:$recode \n";
-    echo "cipher DER: ".string2hex($der)." len:".strlen($der)." \n";
-
-    //解密
-    $php_func = 'php_SM2Decrypt';
-    $recode = $php_func($cipher, $plain,$sm2keys[0]);
-    echo "recode:$recode \n";
-    echo "plain: $plain\n";
-
-    //php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式
-    //则通过如下方式转换,然后再传入接口解密
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(101,$der, $c1c3c2);
-    echo "recode:$recode \n";
-    echo "cipher c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
-}
-
-//test php_HextoAsc php_AsctoHex
-function test_php_HextoAscAndAsctoHex(){
-    $php_func = 'php_AsctoHex';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-
-    $recode = $php_func($data, $hex);
-    echo "recode:$recode \n";
-    echo "hex: $hex len:".strlen($hex)." \n";
-    $php_func = 'php_HextoAsc';
-    $recode = $php_func($hex, $asc);
-    echo "recode:$recode \n";
-    echo "plain: $asc\n";
-}
-
-//test php_base64_decode php_base64_encode
-function test_php_base64_decodeAndbase64_encode(){
-    $php_func = 'php_base64_encode';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-
-    $recode = $php_func($data, $base64);
-    echo "recode:$recode \n";
-    echo "base64char: $base64 len:".strlen($base64)." \n";
-    $php_func = 'php_base64_decode';
-    $recode = $php_func($base64, $orgin);
-    echo "recode:$recode \n";
-    echo "plain: $orgin\n";
-}
-
-//test
-function test_php_SM2FormatConvert(){
-    $php_func = 'php_SM2FormatConvert';
-    echo "--------$php_func--------\n";
-    //hex编码的DER密文
-    $data =
-        "308198021F5A9D5395EE7A52463E07727CAEA3001A3D95ADF105992B3F8430C0B63D5272022011BC87B586EA976FF2A8009393FE3F71FEBD11FD249F59796054DB66B4789C6D04201D3FAEF883C8D839DDF36FC083B125DE65D8E58B3AACEB97CEBCF529A8F0C2A7043119C9E04B8DB90E6CC0898A85665DA1FA990B4DB6197AE55674D68C3AB5328D408D0B7EBAE99ED6C8243FED2A18C024545A";
-    //先解码再转der格式(如果是base64编码,则用base64解码)
-    $php_HextoAsc = 'php_HextoAsc';
-    $recode = $php_HextoAsc($data, $dataAsc);
-    $recode = $php_func(101,$dataAsc, $c1c3c2);
-    echo "recode:$recode \n";
-    echo "c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
-}
-
-//test SM4CBCCrypt
-function test_php_SM4CBCCrypt(){
-    $php_func = 'php_SM4CBCCrypt';
-    echo "--------$php_func--------\n";
-    $data = "this is a test! 这是个测试!";
-    $plainkey = "1234567890123456";//如果是hex编码,务必解码
-
-    //接口是密文key,将明文key加密后再传入php_SM4CBCCrypt进行数据加密
-    //不必要每次都加密key,建议手工加密一次,将key密文保存使用,key明文保存至安全的地方
-    //若是会话级别的key则自行定策略
-    $php_CryptKey = 'php_CryptKey';
-    $recode = $php_CryptKey(0, $plainkey,$key);
-    echo "recode:$recode \n";
-
-    //打印key密文
-    $php_hex = 'php_AsctoHex';
-    $recode = $php_hex($key, $hex);
-    echo "recode:$recode \n";
-    echo "hex: $hex len:".strlen($hex)." \n";
-
-    $iv = "1234567812345678";
-    //加密
-    $recode = $php_func(0,$data,$redata,$key,$iv);
-    echo "recode:$recode \n";
-    echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    //解密
-    $recode = $php_func(1,$redata,$dedata,$key,$iv);
-    echo "recode:$recode \n";
-    echo "plain: ".$dedata." \n";
-}
-
-function run_test(){
-    //先初始化接口,初始化成功后,可重复调用加解密接口
-    test_php_crypto_init();
-
-    test_php_SM4Crypt1();
-    echo "\n";
-    test_php_SM4Crypt2();
-    echo "\n";
-    test_php_CryptFile();
-    echo "\n";
-    test_php_CryptKey();
-    echo "\n";
-    test_php_SM3Crypt();
-    echo "\n";
-    test_php_MACCrypt();
-    echo "\n";
-    test_php_CryptLmkMac();
-    echo "\n";
-    test_php_SM2SignAndSM2Verify();
-    echo "\n";
-    test_php_SM2SignHashAndSM2VerifyHash();
-    echo "\n";
-    test_php_SM2EncryptAndSM2Decrypt();
-    echo "\n";
-    test_php_HextoAscAndAsctoHex();
-    echo "\n";
-    test_php_base64_decodeAndbase64_encode();
-    echo "\n";
-    test_php_SM2FormatConvert();
-    echo "\n";
-    test_php_SM4CBCCrypt();
-}
-run_test();
-?>

+ 2 - 2
app/api/controller/PublicController.php

@@ -16,6 +16,7 @@ use app\models\user\UserLevel;
 use app\models\user\WechatUser;
 use app\Request;
 use crmeb\services\CacheService;
+use crmeb\services\GmService;
 use crmeb\services\SystemConfigService;
 use crmeb\services\UtilService;
 use crmeb\services\workerman\ChannelService;
@@ -35,8 +36,7 @@ class PublicController
 
     public function test()
     {
-        $gmd = new Gmdemo();
-        $gmd->_initialize();
+        GmService::_initialize();
     }
 
     /**

+ 0 - 343
app/api/controller/phpcrypto.php

@@ -1,343 +0,0 @@
-<?php
-function string2hex($string){
-    $hex = '';
-    for($i = 0;$i < strlen($string); $i++){
-        $ch = dechex(ord($string[$i]));
-        if(strlen($ch)==1){
-            $ch = "0".$ch;
-        }
-        $hex .=$ch;
-    }
-    return $hex;
-}
-/*
-function hex2string($hex){
-	//echo "$hex \n";
-	$string = '';
-	for($i = 0;$i < strlen($hex);$i++){
-		$string .= chr(hexdec($hex[$i].$hex[$i++]));
-	}
-	return $string;
-}
-*/
-if(!extension_loaded('phpcrypto')) {
-    dl('phpcrypto.so');
-}
-
-$module = 'phpcrypto';
-$functions = get_extension_funcs($module);
-echo "Functions available in the test extension:\n";
-foreach($functions as $func) {
-    echo $func."\n";
-}
-echo "\n";
-
-//先加载动态库,否则无法调用接口
-function test_php_crypto_init(){
-    $php_func = 'php_crypto_init';
-    echo "--------$php_func--------\n";
-    $path = "/www/server/php/73/libcryptAPIsm_lnx64.so";//国密动态库路径
-    $recode = $php_func($path);
-    echo "recode:$recode \n";//如果非零,则加载失败
-}
-
-//test php_SM4Crypt1
-function test_php_SM4Crypt1(){
-    $php_func = 'php_SM4Crypt1';
-    echo "--------$php_func--------\n";
-    $data = "this is a test! 这是个测试!";
-    $key = "1234567890123456";//如果是hex编码,务必解码
-    $recode = $php_func(0,$data,$redata,$key);
-    echo "recode:$recode \n";
-    echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $recode = $php_func(1,$redata,$dedata,$key);
-    echo "recode:$recode \n";
-    echo "plain: ".$dedata." \n";
-}
-
-//test SM4Crypt2
-function test_php_SM4Crypt2(){
-    $php_func = 'php_SM4Crypt2';
-    echo "--------$php_func--------\n";
-    $data = "this is a test! 这是个测试!";
-    $key = "1234567890123456";//如果是hex编码,务必解码
-    $recode = $php_func(0,$data,$redata,$key);
-    echo "recode:$recode \n";
-    echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $recode = $php_func(1,$redata,$dedata,$key);
-    echo "recode:$recode \n";
-    echo "plain: ".$dedata." \n";
-}
-
-//test php_CryptFile
-function test_php_CryptFile(){
-    $php_func = 'php_CryptFile';
-    echo "--------$php_func--------\n";
-    $flag = 2;
-    $infilename = "/home/essc50mysql/mf/src_crypt/test_cryptfile";
-    $encodefilename = "/home/essc50mysql/mf/src_crypt/test_encode_cryptfile";
-    $format = 1;
-    $key = "1234567890123456";//如果是hex编码,务必解码
-    $recode = $php_func($flag, $key, $infilename,$encodefilename,$format);
-    echo "recode:$recode \n";
-    $flag = 3;
-    $decodefilename = "/home/essc50mysql/mf/src_crypt/test_decode_cryptfile";
-    $recode = $php_func($flag, $key, $encodefilename,$decodefilename,$format);
-    echo "recode:$recode \n";
-}
-
-//test php_CryptKey
-function test_php_CryptKey(){
-    $php_func = 'php_CryptKey';
-    echo "--------$php_func--------\n";
-    $flag = 0;
-    $plainkey = "1234567890123456";
-    $recode = $php_func($flag, $plainkey,$cipherkey);
-    echo "recode:$recode \n";
-    echo "cipherkey: ".string2hex($cipherkey)." len:".strlen($cipherkey)." \n";
-    $flag = 2;
-    $recode = $php_func($flag, NULL,$genkey);
-    echo "recode:$recode \n";
-    echo "genkey: ".string2hex($genkey)." len:".strlen($genkey)." \n";
-}
-
-//test php_SM3Crypt
-function test_php_SM3Crypt(){
-    $php_func = 'php_SM3Crypt';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!";
-    $recode = $php_func($data,$sm3hash);
-    echo "recode:$recode \n";
-    echo "sm3hash=================: ".string2hex($sm3hash)." len:".strlen($sm3hash)."============= \n";
-}
-
-//test php_MACCrypt
-function test_php_MACCrypt(){
-    $php_func = 'php_MACCrypt';
-    echo "--------$php_func--------\n";
-    $flag = 0;
-    $data = "this is test 这是测试!this is test 这是测试!";
-    $key = "1234567890123456";
-    $recode = $php_func($flag,$data, $key,$redata);
-    echo "recode:$recode \n";
-    echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $flag = 1;
-    $recode = $php_func($flag,$data, $key,$redata);
-    echo "recode:$recode \n";
-    echo "flag:".$flag." redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-}
-
-//test php_CryptLmkMac
-function test_php_CryptLmkMac(){
-    $php_func = 'php_CryptLmkMac';
-    echo "--------$php_func--------\n";
-    $recode = $php_func($data,$version, $pubkey);
-    echo "recode:$recode \n";
-    echo "keymac: ".string2hex($data)." version:".$version." pubkey".string2hex($pubkey)." len:".strlen($pubkey)." \n";
-}
-
-//test php_SM2Genkey
-function test_php_SM2Genkey(){
-    $php_func = 'php_SM2Genkey';
-    //echo "--------$php_func--------\n";
-    $recode = $php_func($privkey, $pubkey);
-    //echo "recode:$recode \n";
-    echo "privkey: ".string2hex($privkey)." len:".strlen($privkey)." pubkey: ".string2hex($pubkey)." len:".strlen($pubkey)." \n";
-
-    echo "sm2密钥对:"+array($privkey, $pubkey);
-
-    return array($privkey, $pubkey);
-}
-
-
-//test php_SM2Sign
-function test_php_SM2SignAndSM2Verify(){
-    $php_func = 'php_SM2Sign';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";//数据有中文则需要注意字符编码的问题
-    $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
-    //签名
-    $recode = $php_func($data, $redata,$sm2keys[0],$sm2keys[1]);
-    echo "recode:$recode \n";
-    echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
-    //php_SM2Sign接口返回的签名值是RS格式,以下示例转DER编码
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(202,$redata, $der);
-    echo "recode:$recode \n";
-    echo "signValue DER: ".string2hex($der)." len:".strlen($der)." \n";
-
-    //验签
-    $php_func = 'php_SM2Verify';
-    $recode = $php_func($data, $redata,$sm2keys[1]);
-    echo "recode:$recode \n";
-
-    //php_SM2Verify 接收的签名值是RS格式,若对方给的签名值是DER格式
-    //则通过如下方式转换,然后再验签
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(201,$der, $rs);
-    echo "recode:$recode \n";
-    echo "signValue RS: ".string2hex($rs)." len:".strlen($rs)." \n";
-}
-
-//test php_SM2SignHash php_SM2VerifyHash
-function test_php_SM2SignHashAndSM2VerifyHash(){
-    $php_func = 'php_SM2SignHash';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-    $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
-
-    $php_func = 'php_SM3Crypt';
-    $recode = $php_func($data,$sm3hash);
-    echo "recode:$recode \n";
-    echo "sm3hash: ".string2hex($sm3hash)." len:".strlen($sm3hash)." \n";
-
-    $php_func = 'php_SM2SignHash';
-    $recode = $php_func($sm3hash, $redata,$sm2keys[0]);
-    echo "recode:$recode \n";
-    echo "signValue: ".string2hex($redata)." len:".strlen($redata)." \n";
-    $php_func = 'php_SM2VerifyHash';
-    $recode = $php_func($sm3hash, $redata,$sm2keys[1]);
-    echo "recode:$recode \n";
-}
-
-//test php_SM2Encrypt php_SM2Decrypt
-function test_php_SM2EncryptAndSM2Decrypt(){
-    $php_func = 'php_SM2Encrypt';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-    $sm2keys = test_php_SM2Genkey();//0 privkey 1 pubkey
-
-    //加密
-    $recode = $php_func($data, $cipher,$sm2keys[1]);
-    echo "recode:$recode \n";
-    echo "cipher: ".string2hex($cipher)." len:".strlen($cipher)." \n";
-    //php_SM2Encrypt接口返回的密文值是c1c3c2格式,以下示例转DER编码
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(102,$cipher, $der);
-    echo "recode:$recode \n";
-    echo "cipher DER: ".string2hex($der)." len:".strlen($der)." \n";
-
-    //解密
-    $php_func = 'php_SM2Decrypt';
-    $recode = $php_func($cipher, $plain,$sm2keys[0]);
-    echo "recode:$recode \n";
-    echo "plain: $plain\n";
-
-    //php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式
-    //则通过如下方式转换,然后再传入接口解密
-    $php_func = 'php_SM2FormatConvert';
-    $recode = $php_func(101,$der, $c1c3c2);
-    echo "recode:$recode \n";
-    echo "cipher c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
-}
-
-//test php_HextoAsc php_AsctoHex
-function test_php_HextoAscAndAsctoHex(){
-    $php_func = 'php_AsctoHex';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-
-    $recode = $php_func($data, $hex);
-    echo "recode:$recode \n";
-    echo "hex: $hex len:".strlen($hex)." \n";
-    $php_func = 'php_HextoAsc';
-    $recode = $php_func($hex, $asc);
-    echo "recode:$recode \n";
-    echo "plain: $asc\n";
-}
-
-//test php_base64_decode php_base64_encode
-function test_php_base64_decodeAndbase64_encode(){
-    $php_func = 'php_base64_encode';
-    echo "--------$php_func--------\n";
-    $data = "this is test 这是测试!this is test 这是测试!";
-
-    $recode = $php_func($data, $base64);
-    echo "recode:$recode \n";
-    echo "base64char: $base64 len:".strlen($base64)." \n";
-    $php_func = 'php_base64_decode';
-    $recode = $php_func($base64, $orgin);
-    echo "recode:$recode \n";
-    echo "plain: $orgin\n";
-}
-
-//test
-function test_php_SM2FormatConvert(){
-    $php_func = 'php_SM2FormatConvert';
-    echo "--------$php_func--------\n";
-    //hex编码的DER密文
-    $data =
-        "308198021F5A9D5395EE7A52463E07727CAEA3001A3D95ADF105992B3F8430C0B63D5272022011BC87B586EA976FF2A8009393FE3F71FEBD11FD249F59796054DB66B4789C6D04201D3FAEF883C8D839DDF36FC083B125DE65D8E58B3AACEB97CEBCF529A8F0C2A7043119C9E04B8DB90E6CC0898A85665DA1FA990B4DB6197AE55674D68C3AB5328D408D0B7EBAE99ED6C8243FED2A18C024545A";
-    //先解码再转der格式(如果是base64编码,则用base64解码)
-    $php_HextoAsc = 'php_HextoAsc';
-    $recode = $php_HextoAsc($data, $dataAsc);
-    $recode = $php_func(101,$dataAsc, $c1c3c2);
-    echo "recode:$recode \n";
-    echo "c1c3c2: ".string2hex($c1c3c2)." len:".strlen($c1c3c2)." \n";
-}
-
-//test SM4CBCCrypt
-function test_php_SM4CBCCrypt(){
-    $php_func = 'php_SM4CBCCrypt';
-    echo "--------$php_func--------\n";
-    $data = "this is a test! 这是个测试!";
-    $plainkey = "1234567890123456";//如果是hex编码,务必解码
-
-    //接口是密文key,将明文key加密后再传入php_SM4CBCCrypt进行数据加密
-    //不必要每次都加密key,建议手工加密一次,将key密文保存使用,key明文保存至安全的地方
-    //若是会话级别的key则自行定策略
-    $php_CryptKey = 'php_CryptKey';
-    $recode = $php_CryptKey(0, $plainkey,$key);
-    echo "recode:$recode \n";
-
-    //打印key密文
-    $php_hex = 'php_AsctoHex';
-    $recode = $php_hex($key, $hex);
-    echo "recode:$recode \n";
-    echo "hex: $hex len:".strlen($hex)." \n";
-
-    $iv = "1234567812345678";
-    //加密
-    $recode = $php_func(0,$data,$redata,$key,$iv);
-    echo "recode:$recode \n";
-    echo "redata: ".string2hex($redata)." len:".strlen($redata)." \n";
-    //解密
-    $recode = $php_func(1,$redata,$dedata,$key,$iv);
-    echo "recode:$recode \n";
-    echo "plain: ".$dedata." \n";
-}
-
-function run_test(){
-    //先初始化接口,初始化成功后,可重复调用加解密接口
-    test_php_crypto_init();
-
-    test_php_SM4Crypt1();
-    echo "\n";
-    test_php_SM4Crypt2();
-    echo "\n";
-    test_php_CryptFile();
-    echo "\n";
-    test_php_CryptKey();
-    echo "\n";
-    test_php_SM3Crypt();
-    echo "\n";
-    test_php_MACCrypt();
-    echo "\n";
-    test_php_CryptLmkMac();
-    echo "\n";
-    test_php_SM2SignAndSM2Verify();
-    echo "\n";
-    test_php_SM2SignHashAndSM2VerifyHash();
-    echo "\n";
-    test_php_SM2EncryptAndSM2Decrypt();
-    echo "\n";
-    test_php_HextoAscAndAsctoHex();
-    echo "\n";
-    test_php_base64_decodeAndbase64_encode();
-    echo "\n";
-    test_php_SM2FormatConvert();
-    echo "\n";
-    test_php_SM4CBCCrypt();
-}
-run_test();
-?>

+ 2 - 1
composer.json

@@ -39,7 +39,8 @@
         "dh2y/think-qrcode": "^2.0",
         "topthink/think-multi-app": "^1.0",
         "topthink/think-template": "^2.0",
-        "overtrue/pinyin": "^4.0"
+        "overtrue/pinyin": "^4.0",
+      "ext-openssl": "*"
     },
     "autoload": {
         "psr-4": {

+ 72 - 78
app/api/controller/GmController.php → crmeb/services/GmService.php

@@ -14,21 +14,23 @@
  * 9、encrypt   aes加密方法
  */
 
-namespace app\api\controller;
+namespace crmeb\services;
 
 
-use app\api\controller\AuthController as Controller;
+use function halt;
+use function input;
+use function json;
 
-class GmController extends Controller
+class GmService
 {
 
     //以下参数都需要金融云提供
     //AES偏移量
-    protected $iv = 'abcdefghABCDEFGH';
-    protected $appID = "96c8268e-cfa4-497f-9329-a5e3417f0825";
-    protected $appSecretKey = "921b3c06-eaed-7b1c-e053-010000717745";
-    protected $tokenUrl = "https://mouldai.com/apiSIT/tzyj/approveDev";//回归环境
-    protected $token;
+    protected static $iv = 'abcdefghABCDEFGH';
+    protected static $appID = "96c8268e-cfa4-497f-9329-a5e3417f0825";
+    protected static $appSecretKey = "921b3c06-eaed-7b1c-e053-010000717745";
+    protected static $tokenUrl = "https://mouldai.com/apiSIT/tzyj/approveDev";//回归环境
+    protected static $token;
     //合作机构自有公钥
     const PUBLIC_KEY = "DE3w4DgjV+np0oHQzSDUgvlxGKPhi/gHBRp8dtjun+z6uqrRJE6B1qswZpaSCs3tp0tm98ZjjL9RTuNh4dyUuA==";
     //合作机构自有私钥
@@ -36,16 +38,16 @@ class GmController extends Controller
     //泰隆银行公钥
     const CGB_PUBLIC_KEY = "kZYtvaa+HIImu0xhONxte8wyXmMBe7HKKf1i1fqU7Wf8PrsmvAguPNClPXfOoxqW8gmy5qrFK9oX3tvmrq4r0A==";
 
-    public function _initialize()
+    public static function _initialize()
     {
         // 初始化加密扩展
-        echo "--------php_crypto_init 初始化开始--------\n";
+//        echo "--------php_crypto_init 初始化开始--------\n";
         $path = "/www/server/php/73/libcryptAPIsm_lnx64.so";
         php_crypto_init($path);
-        echo "--------php_crypto_init 初始化结束-------\n";
+//        echo "--------php_crypto_init 初始化结束-------\n";
     }
 
-    public function string2hex($string)
+    public static function string2hex($string)
     {
         $hex = '';
         for ($i = 0; $i < strlen($string); $i++) {
@@ -59,49 +61,49 @@ class GmController extends Controller
     }
 
     //获取token
-    public function getToken()
+    public static function getToken()
     {
         //获取6位数字符串
         $seqNO = (string)rand(100000, 999999);
         //获取16位随机字符串并md5 转大写 得到
-        $key = strtoupper(md5($this->getKey()));
+        $key = strtoupper(md5(self::getKey()));
 
 
         //echo $this->string2hex(base64_decode(self::PRIVATE_KEY));
 
-        echo $this->string2hex(base64_decode(self::PUBLIC_KEY));
+        echo self::string2hex(base64_decode(self::PUBLIC_KEY));
 
 
         //初始化转换密钥信息
-        php_HextoAsc($this->string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
-        php_HextoAsc($this->string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
-        php_HextoAsc($this->string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
+        php_HextoAsc(self::string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
+        php_HextoAsc(self::string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
+        php_HextoAsc(self::string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
 
         //拼接参数数组
         $data = [
-            'appID' => $this->appID,
+            'appID' => self::$appID,
             'seqNO' => $seqNO,
             'random' => strtoupper(md5($seqNO)),
-            'sm2EncryptData' => $this->SM2Encrypt($key, $gf_k), //sm2加密key
-            'sm2Sign' => $this->SM2Sign($key, $k2, $k1), //sm2加密key
+            'sm2EncryptData' => self::SM2Encrypt($key, $gf_k), //sm2加密key
+            'sm2Sign' => self::SM2Sign($key, $k2, $k1), //sm2加密key
         ];
 
         //拼接签名参数,md5转大写
-        $data['sign'] = $this->SM3Crypt($data['random'] . $data['seqNO'] . $this->appSecretKey . $key);
+        $data['sign'] = self::SM3Crypt($data['random'] . $data['seqNO'] . self::$appSecretKey . $key);
 
         //发送post接口请求
-        $res = $this->https_post($this->tokenUrl, $data);
+        $res = self::https_post(self::$tokenUrl, $data);
 
         //解密返回token数据
-        $token = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
+        $token = self::SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
 
-        $this->token = $token;
+        self::$token = $token;
         //返回token
         return $token;
     }
 
     //业务请求数据
-    public function postJry()
+    public static function postJry()
     {
         //创建模拟请求数据,必须包含head,body
         $data = [
@@ -113,22 +115,19 @@ class GmController extends Controller
                 'msg' => "测试请求"
             ]
         ];
-        $res = $this->scanPaymentCode($data);
+        $res = self::scanPaymentCode($data);
         dump("返回结果");
         halt($res);
 
     }
 
     //业务请求封装
-    public function scanPaymentCode($array = [])
+    public static function scanPaymentCode($array = [])
     {
         //初始化转换密钥信息
-        php_HextoAsc($this->string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
-        php_HextoAsc($this->string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
-        php_HextoAsc($this->string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
-//        $this->string2hex(base64_decode(self::PRIVATE_KEY));
-//        $this->string2hex(base64_decode(self::PUBLIC_KEY));
-//        $this->string2hex(base64_decode(self::CGB_PUBLIC_KEY));
+        php_HextoAsc(self::string2hex(base64_decode(self::PRIVATE_KEY)), $k1);
+        php_HextoAsc(self::string2hex(base64_decode(self::PUBLIC_KEY)), $k2);
+        php_HextoAsc(self::string2hex(base64_decode(self::CGB_PUBLIC_KEY)), $gf_k);
 
 
         //数据转json
@@ -136,41 +135,41 @@ class GmController extends Controller
         //获取随机6位字符串数字
         $seqNO = (string)rand(100000, 999999);
         //获取随机字符串秘钥,md5 并转大写
-        $key = strtoupper(md5($this->getKey()));
+        $key = strtoupper(md5(self::getKey()));
         //拼接请求数据
         $data = [
-            'appID' => $this->appID,
+            'appID' => self::$appID,
             'seqNO' => $seqNO,
             'signMethod' => "SM3",
             'encryptMethod' => "SM4",
-            'appAccessToken' => $this->getToken(),//获取token
-            'sm2EncryptData' => $this->SM2Encrypt($key, $gf_k), //sm2加密key
-            'sm2Sign' => $this->SM2Sign($key, $k2, $k1), //sm2加密key
+            'appAccessToken' => self::getToken(),//获取token
+            'sm2EncryptData' => self::SM2Encrypt($key, $gf_k), //sm2加密key
+            'sm2Sign' => self::SM2Sign($key, $k2, $k1), //sm2加密key
         ];
 
         //拼接签名参数,md5转大写
-        $data['sign'] = $this->SM3Crypt($json . $data['seqNO'] . $this->appSecretKey . $key);
+        $data['sign'] = self::SM3Crypt($json . $data['seqNO'] . self::$appSecretKey . $key);
         //AES加密业务数据
-        $data['reqData'] = $this->SM4Encrypt($json, $data['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $key);
+        $data['reqData'] = self::SM4Encrypt($json, $data['seqNO'] . $data['appAccessToken'] . self::$appSecretKey . $key);
 
         dump("业务数据json");
         dump($json);
         dump("请求数据");
         dump($data);
-        $res = $this->https_post("https://mouldai.com/apiSIT/tzyj/scanPaymentCode", $data);
+        $res = self::https_post("https://mouldai.com/apiSIT/tzyj/scanPaymentCode", $data);
 
         //解密sm2密钥
-        $sm2Key = $this->SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
+        $sm2Key = self::SM2Decrypt(base64_decode($res['sm2EncryptData']), $k1);
 
         dump("sm2解密数据:" . $sm2Key);
 
         //SM4解密数据
-        $rspData = $this->SM4Decrypt($res['rspData'], $res['seqNO'] . $data['appAccessToken'] . $this->appSecretKey . $sm2Key);
+        $rspData = self::SM4Decrypt($res['rspData'], $res['seqNO'] . $data['appAccessToken'] . self::$appSecretKey . $sm2Key);
 
         dump("sm4解密报文数据:" . $rspData);
 
         //sm3验签数据校验
-        $sm3SignData = $this->SM3Crypt($rspData . $res['seqNO'] . $this->appSecretKey . $sm2Key);
+        $sm3SignData = self::SM3Crypt($rspData . $res['seqNO'] . self::$appSecretKey . $sm2Key);
 
         dump("sm3响应报文拼接加签:" . $sm3SignData);
 
@@ -187,7 +186,7 @@ class GmController extends Controller
     }
 
     //获取post数据并解密解签获得业务数据
-    public function getPostData()
+    public static function getPostData()
     {
         //获取post数据
         $arr = input("post.");
@@ -197,16 +196,16 @@ class GmController extends Controller
 
         //检验字段是否缺失
         if (!isset($arr['rsaEncryptData']) || !isset($arr['seqNO']) || !isset($arr['signMethod']) || !isset($arr['sign']) || !isset($arr['encryptMethod']) || !isset($arr['reqData']) || !isset($arr['appID'])) {
-            return $this->runJson("1000001", "缺少参数");
+            return self::runJson("1000001", "缺少参数");
         }
         dump("获取到的post数据");
         dump($arr);
         //rsa解密秘钥字段rsaEncryptData
-        $rasKey = $this->decryptByPublicKey($arr['rsaEncryptData']);
+        $rasKey = self::decryptByPublicKey($arr['rsaEncryptData']);
         dump("解密后的秘钥");
         dump($rasKey);
         //拼接秘钥
-        $key = $arr['seqNO'] . $this->token . $this->appSecretKey . $rasKey;
+        $key = $arr['seqNO'] . self::$token . self::$appSecretKey . $rasKey;
 
         //秘钥转md5 转大写
         $key = strtoupper(md5($key));
@@ -220,7 +219,7 @@ class GmController extends Controller
     }
 
     //SM2Encrypt
-    function SM2Encrypt($data, $pubkey)
+    public static function SM2Encrypt($data, $pubkey)
     {
         //sm2加密
         $recode = php_SM2Encrypt($data, $cipher, $pubkey);
@@ -240,7 +239,7 @@ class GmController extends Controller
     }
 
     //SM2Decrypt
-    function SM2Decrypt($data, $privkey)
+    public static function SM2Decrypt($data, $privkey)
     {
         //php_SM2Decrypt 接收密文值是c1c3c2格式,若对方给的密文值是DER格式 则通过如下方式转换,然后再传入接口解密
         $recode = php_SM2FormatConvert(101, $data, $c1c3c2);
@@ -254,7 +253,7 @@ class GmController extends Controller
     }
 
     //SM2Sign
-    function SM2Sign($data, $pubkey, $privkey)
+    public static function SM2Sign($data, $pubkey, $privkey)
     {
         //签名
         $recode = php_SM2Sign($data, $redata, $privkey, $pubkey);
@@ -273,16 +272,16 @@ class GmController extends Controller
     }
 
     //SM3Crypt
-    function SM3Crypt($data)
+    public static function SM3Crypt($data)
     {
         $recode = php_SM3Crypt($data, $sm3hash);
         echo "<br/>recode:$recode \n";
         //加签数据base64返回
-        return strtoupper($this->string2hex($sm3hash));
+        return strtoupper(self::string2hex($sm3hash));
     }
 
     //16进制转换为二进制
-    function hex2String($hexdata)
+    public static function hex2String($hexdata)
     {
         $bindata = "";
         for ($i = 0; $i < strlen($hexdata); $i += 2) {
@@ -292,7 +291,7 @@ class GmController extends Controller
     }
 
     //SM4Encrypt
-    function SM4Encrypt($data, $password)
+    public static function SM4Encrypt($data, $password)
     {
         //加密密码做特殊处理  先MD5转换大写 从第8位开始截取16个字符串
         $password = substr(strtoupper(md5($password)), 8, 16);
@@ -305,7 +304,7 @@ class GmController extends Controller
         echo "<br/>recode:$recode \n";
 
         //加密
-        $recode = php_SM4CBCCrypt(0, $data, $redata, $key, $this->iv);
+        $recode = php_SM4CBCCrypt(0, $data, $redata, $key, self::$iv);
         echo "<br/>recode:$recode \n";
         echo "<br/>redata: " . base64_encode($redata) . " \n";
 
@@ -314,7 +313,7 @@ class GmController extends Controller
     }
 
     //SM4Encrypt
-    function SM4Decrypt($data, $password)
+    public static function SM4Decrypt($data, $password)
     {
         echo "<br/>SM4解密数据=======" . $data;
 
@@ -329,37 +328,37 @@ class GmController extends Controller
         echo "<br/>recode:$recode \n";
 
         //解密
-        $recode = php_SM4CBCCrypt(1, base64_decode($data), $dedata, $key, $this->iv);
+        $recode = php_SM4CBCCrypt(1, base64_decode($data), $dedata, $key, self::$iv);
         echo "<br/>sm4[解密]recode:$recode \n";
         return $dedata;
     }
 
     /**
      * 公钥解密
-     * @param 密文数据 $data
-     * @return 原文结果 string
+     * @param mixed $data 密文数据
+     * @return string 原文结果
      */
-    private function decryptByPublicKey($data)
+    private static function decryptByPublicKey($data)
     {
         $data = base64_decode($data);
-        openssl_public_decrypt($data, $decrypted, $this->publicKey, OPENSSL_PKCS1_PADDING);//公钥解密
+        openssl_public_decrypt($data, $decrypted, self::PUBLIC_KEY, OPENSSL_PKCS1_PADDING);//公钥解密
         return $decrypted;
     }
 
     /**
      * 私钥加密
-     * @param 原始数据 $data
-     * @return 密文结果 string
+     * @param mixed $data 原始数据
+     * @return string 密文结果
      */
-    private function encryptByPrivateKey($data)
+    private static function encryptByPrivateKey($data)
     {
-        openssl_private_encrypt($data, $encrypted, $this->privateKey, OPENSSL_PKCS1_PADDING);//私钥加密
-        $encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
-        return $encrypted;
+        openssl_private_encrypt($data, $encrypted, self::PRIVATE_KEY, OPENSSL_PKCS1_PADDING);//私钥加密
+        //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
+        return base64_encode($encrypted);
     }
 
     //封装返回数据
-    private function runJson($code = "000000", $msg = "完成")
+    private static function runJson($code = "000000", $msg = "完成")
     {
         return json(['code' => $code, 'message' => $msg]);
 
@@ -380,7 +379,7 @@ class GmController extends Controller
     }
 
     // curl post请求
-    private function https_post($url, $data = null)
+    private static function https_post($url, $data = null)
     {
         $data = json_encode($data, JSON_UNESCAPED_SLASHES);
         $header [] = 'Content-Type:application/x-www-form-urlencoded';
@@ -397,23 +396,18 @@ class GmController extends Controller
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $tmpInfo = curl_exec($ch);
         curl_close($ch);
-        $tmpInfo1 = json_decode($tmpInfo, true);
-        return $tmpInfo1;
+        return json_decode($tmpInfo, true);
     }
 
     /**
      * 获得随机字符串
      **/
-    private function getKey($length = 16)
+    private static function getKey($length = 16)
     {
         // 密码字符集,可任意添加你需要的字符
         $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ2345678';
         $key = '';
         for ($i = 0; $i < $length; $i++) {
-            // 这里提供两种字符获取方式
-            // 第一种是使用 substr 截取$chars中的任意一位字符;
-            // 第二种是取字符数组 $chars 的任意元素
-            // $key .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
             $key .= $chars[mt_rand(0, strlen($chars) - 1)];
         }
         return $key;