Signature.php
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Lackoxygen\TiktokShop\Support;
use Lackoxygen\TiktokShop\Attribute\Config\Config;
class Signature extends Config
{
/**
* @var string
*/
private string $method;
/**
* @var string
*/
private string $timestamp;
/**
* @var string
*/
private string $paramJson;
/**
* @param mixed $method
*/
public function setMethod($method): Signature
{
$this->method = $method;
return $this;
}
/**
* @param mixed $timestamp
*/
public function setTimestamp($timestamp): Signature
{
$this->timestamp = $timestamp;
return $this;
}
/**
* @param mixed $paramJson
*/
public function setParamJson($paramJson): Signature
{
$this->paramJson = $paramJson;
return $this;
}
/**
* @return string
*/
public function generate()
{
$paramPattern = 'app_key' . $this->config->getAppKey(
) . 'method' . $this->method . 'param_json' . $this->paramJson .
'timestamp' . $this->timestamp . 'v' . $this->config->getVersion();
$body = $this->config->getAppSecret() . $paramPattern . $this->config->getAppSecret();
switch ($this->config->getSignMethod()) {
case 'hmac-sha256':
return $this->sha256($body);
case 'md5':
return $this->md5($body);
default:
throw new \InvalidArgumentException('Signature method not supported');
}
}
/**
* @param string $body
* @return false|string
*/
public function sha256(string $body): string
{
return (string)\hash_hmac("sha256", $body, $this->config->getAppSecret());
}
/**
* @param string $body
* @return string
*/
public function md5(string $body): string
{
return \md5($body);
}
}