Signature.php
2.7 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
91
92
93
94
95
96
97
98
<?php
namespace Upyun;
/**
* Class Signature
* @package Upyun
*/
class Signature
{
/**
* 获取分块上传接口的签名
*/
const SIGN_MULTIPART = 1;
/**
* 生成视频处理接口的签名
*/
const SIGN_VIDEO = 2;
/**
* 生成视频处理接口的签名(不需要操作员时使用)
*/
const SIGN_VIDEO_NO_OPERATOR = 3;
/**
* 获取 Header 签名需要的请求头
*
* @param Config $serviceConfig
* @param $method 请求方法
* @param $path 请求路径
* @param $contentMd5 文件内容 md5
*
* @return array
*/
public static function getHeaderSign($serviceConfig, $method, $path, $contentMd5 = null)
{
$gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
$policy = null;
$sign = self::getBodySignature($serviceConfig, $method, $path, $gmtDate, $policy, $contentMd5);
$headers = array(
'Authorization' => $sign,
'X-Date' => $gmtDate,
'User-agent' => 'Php-Sdk/' . $serviceConfig->getVersion()
);
return $headers;
}
/**
* 获取请求缓存刷新接口需要的签名头
*
* @param Config $serviceConfig
* @param $urlString
*
* @return array
*/
public static function getPurgeSignHeader(Config $serviceConfig, $urlString)
{
$gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
$sign = md5("$urlString&{$serviceConfig->serviceName}&$gmtDate&{$serviceConfig->operatorPassword}");
return array(
'Authorization' => "UpYun {$serviceConfig->serviceName}:{$serviceConfig->operatorName}:$sign",
'Date' => $gmtDate,
'User-agent' => 'Php-Sdk/' . $serviceConfig->getVersion() . ' (purge api)'
);
}
/**
* 获取表单 API 需要的签名,依据 body 签名规则计算
* @param Config $serviceConfig
* @param $method 请求方法
* @param $uri 请求路径
* @param $date 请求时间
* @param $policy
* @param $contentMd5 请求 body 的 md5
*
* @return array
*/
public static function getBodySignature(Config $serviceConfig, $method, $uri, $date = null, $policy = null, $contentMd5 = null)
{
$data = array(
$method,
$uri
);
if ($date) {
$data[] = $date;
}
if ($policy) {
$data[] = $policy;
}
if ($contentMd5) {
$data[] = $contentMd5;
}
$signature = base64_encode(hash_hmac('sha1', implode('&', $data), $serviceConfig->operatorPassword, true));
return 'UPYUN ' . $serviceConfig->operatorName . ':' . $signature;
}
}