@@ -21,10 +21,10 @@ class Signature {
2121 const SIGN_VIDEO_NO_OPERATOR = 3 ;
2222
2323 /**
24- * 获取 Header 签名
24+ * 获取 Header 签名需要的请求头
2525 *
2626 * @param Config $bucketConfig
27- * @param $method
27+ * @param $method 请求方法
2828 * @param $path 请求路径
2929 * @param $contentMd5 文件内容 md5
3030 *
@@ -33,20 +33,11 @@ class Signature {
3333 public static function getHeaderSign ($ bucketConfig , $ method , $ path , $ contentMd5 = null ) {
3434 $ gmtDate = gmdate ('D, d M Y H:i:s \G\M\T ' );
3535
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 );
4738
4839 $ headers = array (
49- 'Authorization ' => " UPYUN { $ bucketConfig -> operatorName } : $ sign" ,
40+ 'Authorization ' => $ sign ,
5041 'Date ' => $ gmtDate ,
5142 'User-agent ' => 'Php-Sdk/ ' . $ bucketConfig ->getVersion ()
5243 );
@@ -74,35 +65,32 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
7465 /**
7566 * 获取表单 API 需要的签名,依据 body 签名规则计算
7667 * @param Config $bucketConfig
77- * @param $data
68+ * @param $method 请求方法
69+ * @param $uri 请求路径
70+ * @param $date 请求时间
71+ * @param $policy
72+ * @param $contentMd5 请求 body 的 md5
7873 *
7974 * @return array
8075 */
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
8780 );
88- if (isset ( $ data [ ' date ' ]) ) {
89- $ signParams [ ' date ' ] = $ data [ ' date ' ] ;
81+ if ($ date ) {
82+ $ data [ ] = $ date ;
9083 }
9184
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+ }
10388
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 ;
10694 }
10795
10896 public static function getSignature (Config $ bucketConfig , $ data , $ type , $ tokenSecret = '' ) {
0 commit comments