@@ -21,10 +21,10 @@ class Signature {
21
21
const SIGN_VIDEO_NO_OPERATOR = 3 ;
22
22
23
23
/**
24
- * 获取 Header 签名
24
+ * 获取 Header 签名需要的请求头
25
25
*
26
26
* @param Config $bucketConfig
27
- * @param $method
27
+ * @param $method 请求方法
28
28
* @param $path 请求路径
29
29
* @param $contentMd5 文件内容 md5
30
30
*
@@ -33,20 +33,11 @@ class Signature {
33
33
public static function getHeaderSign ($ bucketConfig , $ method , $ path , $ contentMd5 = null ) {
34
34
$ gmtDate = gmdate ('D, d M Y H:i:s \G\M\T ' );
35
35
36
- $ signParams = array (
37
- $ method ,
38
- $ path ,
39
- $ gmtDate
40
- );
41
-
42
- if ($ contentMd5 ) {
43
- $ signParams [] = $ contentMd5 ;
44
- }
45
-
46
- $ sign = self ::calcSignature ($ bucketConfig , $ signParams );
36
+ $ policy = null ;
37
+ $ sign = self ::getBodySignature ($ bucketConfig , $ method , $ path , $ gmtDate , $ policy , $ contentMd5 );
47
38
48
39
$ headers = array (
49
- 'Authorization ' => " UPYUN { $ bucketConfig -> operatorName } : $ sign" ,
40
+ 'Authorization ' => $ sign ,
50
41
'Date ' => $ gmtDate ,
51
42
'User-agent ' => 'Php-Sdk/ ' . $ bucketConfig ->getVersion ()
52
43
);
@@ -74,35 +65,32 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
74
65
/**
75
66
* 获取表单 API 需要的签名,依据 body 签名规则计算
76
67
* @param Config $bucketConfig
77
- * @param $data
68
+ * @param $method 请求方法
69
+ * @param $uri 请求路径
70
+ * @param $date 请求时间
71
+ * @param $policy
72
+ * @param $contentMd5 请求 body 的 md5
78
73
*
79
74
* @return array
80
75
*/
81
- public static function getFormSignature (Config $ bucketConfig , $ data ) {
82
- $ data ['bucket ' ] = $ bucketConfig ->bucketName ;
83
- $ policy = Util::base64Json ($ data );
84
- $ signParams = array (
85
- 'method ' => 'POST ' ,
86
- 'uri ' => '/ ' . $ bucketConfig ->bucketName ,
76
+ public static function getBodySignature (Config $ bucketConfig , $ method , $ uri , $ date = null , $ policy = null , $ contentMd5 = null ) {
77
+ $ data = array (
78
+ $ method ,
79
+ $ uri
87
80
);
88
- if (isset ( $ data [ ' date ' ]) ) {
89
- $ signParams [ ' date ' ] = $ data [ ' date ' ] ;
81
+ if ($ date ) {
82
+ $ data [ ] = $ date ;
90
83
}
91
84
92
- $ signParams ['policy ' ] = $ policy ;
93
- if (isset ($ data ['content-md5 ' ])) {
94
- $ signParams ['md5 ' ] = $ data ['content-md5 ' ];
95
- };
96
-
97
- $ signature = self ::calcSignature ($ bucketConfig , $ signParams );
98
- return array (
99
- 'policy ' => $ policy ,
100
- 'signature ' => $ signature
101
- );
102
- }
85
+ if ($ policy ) {
86
+ $ data [] = $ policy ;
87
+ }
103
88
104
- private static function calcSignature (Config $ bucketConfig , $ signParams ) {
105
- return base64_encode (hash_hmac ('sha1 ' , implode ('& ' , $ signParams ), $ bucketConfig ->operatorPassword , true ));
89
+ if ($ contentMd5 ) {
90
+ $ data [] = $ contentMd5 ;
91
+ }
92
+ $ signature = base64_encode (hash_hmac ('sha1 ' , implode ('& ' , $ data ), $ bucketConfig ->operatorPassword , true ));
93
+ return 'UPYUN ' . $ bucketConfig ->operatorName . ': ' . $ signature ;
106
94
}
107
95
108
96
public static function getSignature (Config $ bucketConfig , $ data , $ type , $ tokenSecret = '' ) {
0 commit comments