| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace ComplexThree\JuLe;
- /**
- * Class joloRsa
- * rsa加密工具类
- */
- class joloRsa
- {
- /**
- * $orderArr = [];
- * $orderArr['game_name'] = 'game_name'; //游戏名称
- * $orderArr['game_code'] = 'game_code'; //游戏编号(gamecode)
- * $orderArr['game_order_id'] = 'game_order_id';//游戏订单id
- * $orderArr['product_id'] = 'product_id';//商品ID
- * $orderArr['product_name'] = 'product_name';//商品名称
- * $orderArr['product_des'] = 'product_des';//商品描述
- * $orderArr['amount'] = 'amount';//金额,单位为分
- * $orderArr['notify_url'] = 'notify_url';//支付回调地址
- * $orderArr['user_code'] = 'user_code';//用户编号
- * $orderArr['session_id'] = 'session_id';
- * $orderArr['role_name'] = 'role_name';// 游戏角色名
- * $orderArr['role_level'] = 'role_level';// 游戏角色等级
- * $orderArr['area_server'] = 'area_server';// 游戏区服名称
- * $orderArr['vip_level'] = 'vip_level';//游戏vip等级
- * $orderArr['role_id'] = 'role_id';// 游戏角色ID
- * $orderArr['area_server_id'] = 'area_server_id';// 游戏区服ID
- * $orderArr['channel_reserved '] = 'channel_reserved ';//cp自定义字段返回
- * 聚乐订单签名
- */
- // 注意:参数里,不要出现类似“1元=10000个金币”的字段,因为“=”原因,会导致微信支付校验失败
- // 参数值不能出现json串,会导致支付宝扫码支付失败
- // product_id 为字符串类型
- function order_rsa_sign($orderArr, $private_key)
- {
- if (!is_array($orderArr)) {
- return false;
- }
- $pem = chunk_split($private_key, 64, "\n");
- $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
- $res = openssl_get_privatekey($pem);
- if (!$res) {
- return false;
- }
- //参数 JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES $orderArr转JSON格式时中文和斜杠不转义
- openssl_sign(json_encode($orderArr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), $sign, $res);
- openssl_free_key($res);
- $sign = base64_encode($sign);
- return $sign;
- }
- function verification_sign($input, $sign, $pub)
- {
- if (empty($input) || empty($sign) || empty($pub)) {
- return false;
- }
- $pub_key = $this->setupPubKey($pub);
- $res = openssl_get_publickey($pub_key);
- $result = openssl_verify($input, base64_decode($sign), $res);
- if ($res) {
- openssl_free_key($res);
- }
- return $result == 1 ? true : false;
- }
- function setupPubKey($pubKey)
- {
- if (is_resource($pubKey)) {
- return true;
- }
- $pem = chunk_split($pubKey, 64, "\n");
- $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
- $pub_Key = openssl_pkey_get_public($pem);
- return $pub_Key;
- }
- function rsa_verify($data, $signature, $public_key) {
- if(empty($data) || empty($signature)) {
- return false;
- }
- $pub_res = openssl_get_publickey($public_key);
- return openssl_verify($data, base64_decode($signature), $pub_res );
- }
- }
|