PHP code example of lswl / sign

1. Go to this page and download the library: Download lswl/sign 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/ */

    

lswl / sign example snippets


use Lswl\Signature\HttpBuildQuery;
use Lswl\Signature\Json;

// 数据
$data = [
    'key' => 'value',
];
// 密钥
$key = 'key';

// 使用 http_build_query 方式签名
$sign = (new HttpBuildQuery($data, $key))->sign();

// 使用 json 方式签名
$sign = (new Json($data, $key))->sign();

use Lswl\Signature\HttpBuildQuery;
use Lswl\Signature\Algorithms\Hash;

// 签名实例
$sign = new HttpBuildQuery();

// 算法(默认md5)
$algorithm = new Hash();
// 使用 sha256 算法
$algorithm->setAlgorithm('sha256');

// 设置签名数据、密钥、算法
$sign->setData(['key' => 'value'])
    ->setKey('key')
    ->setAlgorithm($algorithm);

// 签名结果字符串
$signStr = $sign->sign();

// 签名步骤数据
$stepArr = $sign->getStep();
// 签名步骤数据字符串
$stepStr = $sign->getStepAsString();

use Lswl\Signature\AbstractSignature;

class Serialize extends AbstractSignature
{
    /**
     * @inheritDoc
     */
    protected function buildSignStr(array $data): string
    {
        return serialize($data);
    }
}

// 数据
$data = [
    'key' => 'value',
];
// 密钥
$key = 'key';
// 使用 serialize 方式签名
$sign = (new Serialize($data, $key))->sign();

use Lswl\Signature\Algorithms\AbstractAlgorithm;
use Lswl\Signature\Json;

class Md5 extends AbstractAlgorithm
{
    /**
     * @inheritDoc
     */
    public function run(string $str): string
    {
        return md5($str);
    }
}

// 数据
$data = [
    'key' => 'value',
];
// 密钥
$key = 'key';
// 使用 json 方式签名, 并设置 Md5 算法
$sign = (new Json($data, $key))
    ->setAlgorithm(new Md5())
    ->sign();