@@ -21,25 +21,34 @@ class Signature {
21
21
const SIGN_VIDEO_NO_OPERATOR = 3 ;
22
22
23
23
/**
24
- * 获取 RESET API 请求需要的签名头
24
+ * 获取 Header 签名
25
25
*
26
26
* @param Config $bucketConfig
27
27
* @param $method
28
- * @param $path
29
- * @param $contentLength
28
+ * @param $path 请求路径
29
+ * @param $contentMd5 文件内容 md5
30
30
*
31
31
* @return array
32
32
*/
33
- public static function getRestApiSignHeader ($ bucketConfig , $ method , $ remotePath , $ contentLength ) {
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
- $ path = '/ ' . $ bucketConfig ->bucketName . '/ ' . ltrim ($ remotePath , '/ ' );
36
35
37
- $ sign = md5 ("$ method& $ path& $ gmtDate& $ contentLength& {$ bucketConfig ->operatorPassword }" );
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 );
38
47
39
48
$ headers = array (
40
- 'Authorization ' => "UpYun {$ bucketConfig ->operatorName }: $ sign " ,
49
+ 'Authorization ' => "UPYUN {$ bucketConfig ->operatorName }: $ sign " ,
41
50
'Date ' => $ gmtDate ,
42
- 'User-agent ' => 'Php-Sdk/ ' . $ bucketConfig ->getVersion () . ' (rest api) '
51
+ 'User-agent ' => 'Php-Sdk/ ' . $ bucketConfig ->getVersion ()
43
52
);
44
53
return $ headers ;
45
54
}
@@ -62,6 +71,13 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
62
71
);
63
72
}
64
73
74
+ /**
75
+ * 获取表单 API 需要的签名,依据 body 签名规则计算
76
+ * @param Config $bucketConfig
77
+ * @param $data
78
+ *
79
+ * @return array
80
+ */
65
81
public static function getFormSignature (Config $ bucketConfig , $ data ) {
66
82
$ data ['bucket ' ] = $ bucketConfig ->bucketName ;
67
83
$ policy = Util::base64Json ($ data );
@@ -78,13 +94,17 @@ public static function getFormSignature(Config $bucketConfig, $data) {
78
94
$ signParams ['md5 ' ] = $ data ['content-md5 ' ];
79
95
};
80
96
81
- $ signature = base64_encode ( hash_hmac ( ' sha1 ' , implode ( ' & ' , $ signParams ), $ bucketConfig-> operatorPassword , true ) );
97
+ $ signature = self :: calcSignature ( $ bucketConfig, $ signParams );
82
98
return array (
83
99
'policy ' => $ policy ,
84
100
'signature ' => $ signature
85
101
);
86
102
}
87
103
104
+ private static function calcSignature ( Config $ bucketConfig , $ signParams ) {
105
+ return base64_encode (hash_hmac ('sha1 ' , implode ('& ' , $ signParams ), $ bucketConfig ->operatorPassword , true ));
106
+ }
107
+
88
108
public static function getSignature ( Config $ bucketConfig , $ data , $ type , $ tokenSecret = '' ) {
89
109
if (is_array ($ data )) {
90
110
ksort ($ data );
0 commit comments