PHP code example of cexll / hyperf-apihash

1. Go to this page and download the library: Download cexll/hyperf-apihash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cexll / hyperf-apihash example snippets


class IndexController 
{
    public function index()
    {
        $cert_path = [
            'public' => BASE_PATH . '/config/cert/public.key',
            'private' => BASE_PATH . '/config/cert/private.pem',
        ];
        // 1. 生成RSA证书 已经生成的可以不用生成, 只需要配置好路径即可
        // 2. 实例化 ApiHash 
        // 3. 选择加密 公钥加密 或 私钥加密 将加密后的body 直接返回即可, 加密后的内容为 
        // [
        //  body 为apibody 
        //  key  为加密key
        //  ]
        // 4. 解密 
        //  解密步骤 1. 通过RSA解密key得到用于加密aes的key, 使用解密后的key, 来解密aes, 例 rsa->decrypt(证书, key) -> key 得到解密后的key, aes->decrypt(body, key) -> body, 得到正常要返回的api body
        $apihash  = new ApiHash();
        $data = '123123123123123123';
        $body = $apihash->privateHash($data, $cert_path['private']);
        $decrypt = $apihash->publicDecrypt($body, $cert_path['public']);
        return $this->response->json([
            'body' => $body,
            'decrypt' => $decrypt
        ]);
    }
}