Files
2026-04-14 17:46:22 +08:00

80 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\common\server\DouGong;
class BaseFunc
{
static function json($data)
{
return json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
}
static function get_data_json(array $data) : array
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = static::json($value);
}
}
return $data;
}
/**
* 私钥加签(对数据源排序),可用于 V2 版本接口数据加签
* @param int $alg 默认 OPENSSL_ALGO_SHA256
*
* @return string 签名串
*/
static function sha_with_rsa_sign($data, $rsaPrivateKey, int $alg = OPENSSL_ALGO_SHA256): string
{
ksort($data);
$data = static::json(static::get_data_json($data));
$key = "-----BEGIN PRIVATE KEY-----\n" .
wordwrap($rsaPrivateKey, 64, "\n", true) .
"\n-----END PRIVATE KEY-----";
$signature = '';
openssl_sign($data, $signature, $key, $alg);
return base64_encode($signature);
}
/**
* 汇付公钥验签(对数据源排序),可用于 V2 版本接口返回数据验签
*
* @param string $signature 签文
* @param string $data 原数据(array)
* @param string $rsaPublicKey 公钥
* @param int $alg 默认 OPENSSL_ALGO_SHA256
*
* @return false|int 验证结果:成功/失败
*/
static function verifySign_sort(string $signature, string $data, string $rsaPublicKey, int $alg = OPENSSL_ALGO_SHA256)
{
$key = "-----BEGIN PUBLIC KEY-----\n" .
wordwrap($rsaPublicKey, 64, "\n", true) .
"\n-----END PUBLIC KEY-----";
return openssl_verify($data, base64_decode($signature), $key, $alg);
}
}