vendor/codeages/rest-api-client/src/Specification/JsonHmacSpecification.php

(开头部分) 1KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

<?php
namespace Codeages\RestApiClient\Specification;

class JsonHmacSpecification implements Specification
{
    protected $algo;

    public function __construct($algo = 'sha1')
    {
        $this->algo = $algo;
    }

    public function getHeaders($token, $requestId = '')
    {
        $headers   = array();
        $headers[] = 'Content-type: application/json';
        $headers[] = "X-Auth-Token: {$token}";
        $headers[] = "X-Request-Id: {$requestId}";
        return $headers;
    }

    public function packToken($config, $url, $body, $deadline, $once)
    {
        $signature = $this->signature($config, $url, $body, $deadline, $once);
        return "{$config['accessKey']}:{$deadline}:{$once}:{$signature}";
    }

    public function unpackToken($string)
    {
        $token = explode(':', $string);
        if (count($token) !== 4) {
            throw new \InvalidArgumentException('token invalid.');
        }

        return array(
            'accessKey' => $token[0],
            'deadline'  => $token[1],
            'once'      => $token[2],
            'signature' => $token[3]
        );
    }

    public function signature($config, $url, $body, $deadline, $once)
    {
        $data      = implode("\n", array($url, $deadline, $once, $body));
        $signature = hash_hmac($this->algo, $data, $config['secretKey'], true);
        $signature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
        return $signature;
    }

    public function serialize($data)
    {
        if (!is_array($data)) {
            throw new \InvalidArgumentException("In json hmac specification serialize data must be array.");
        }

        ksort($data);

        $json = json_encode($data);
        if (JSON_ERROR_NONE !== json_last_error()) {

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。