@@ -21,25 +21,34 @@ class Signature {
2121 const SIGN_VIDEO_NO_OPERATOR = 3 ;
2222
2323 /**
24- * 获取 RESET API 请求需要的签名头
24+ * 获取 Header 签名
2525 *
2626 * @param Config $bucketConfig
2727 * @param $method
28- * @param $path
29- * @param $contentLength
28+ * @param $path 请求路径
29+ * @param $contentMd5 文件内容 md5
3030 *
3131 * @return array
3232 */
33- public static function getRestApiSignHeader ($ bucketConfig , $ method , $ remotePath , $ contentLength ) {
33+ public static function getHeaderSign ($ bucketConfig , $ method , $ path , $ contentMd5 = null ) {
3434 $ gmtDate = gmdate ('D, d M Y H:i:s \G\M\T ' );
35- $ path = '/ ' . $ bucketConfig ->bucketName . '/ ' . ltrim ($ remotePath , '/ ' );
3635
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 );
3847
3948 $ headers = array (
40- 'Authorization ' => "UpYun {$ bucketConfig ->operatorName }: $ sign " ,
49+ 'Authorization ' => "UPYUN {$ bucketConfig ->operatorName }: $ sign " ,
4150 'Date ' => $ gmtDate ,
42- 'User-agent ' => 'Php-Sdk/ ' . $ bucketConfig ->getVersion () . ' (rest api) '
51+ 'User-agent ' => 'Php-Sdk/ ' . $ bucketConfig ->getVersion ()
4352 );
4453 return $ headers ;
4554 }
@@ -62,6 +71,13 @@ public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
6271 );
6372 }
6473
74+ /**
75+ * 获取表单 API 需要的签名,依据 body 签名规则计算
76+ * @param Config $bucketConfig
77+ * @param $data
78+ *
79+ * @return array
80+ */
6581 public static function getFormSignature (Config $ bucketConfig , $ data ) {
6682 $ data ['bucket ' ] = $ bucketConfig ->bucketName ;
6783 $ policy = Util::base64Json ($ data );
@@ -78,14 +94,18 @@ public static function getFormSignature(Config $bucketConfig, $data) {
7894 $ signParams ['md5 ' ] = $ data ['content-md5 ' ];
7995 };
8096
81- $ signature = base64_encode ( hash_hmac ( ' sha1 ' , implode ( ' & ' , $ signParams ), $ bucketConfig-> operatorPassword , true ) );
97+ $ signature = self :: calcSignature ( $ bucketConfig, $ signParams );
8298 return array (
8399 'policy ' => $ policy ,
84100 'signature ' => $ signature
85101 );
86102 }
87103
88- public static function getSignature ( Config $ bucketConfig , $ data , $ type , $ tokenSecret = '' ) {
104+ private static function calcSignature (Config $ bucketConfig , $ signParams ) {
105+ return base64_encode (hash_hmac ('sha1 ' , implode ('& ' , $ signParams ), $ bucketConfig ->operatorPassword , true ));
106+ }
107+
108+ public static function getSignature (Config $ bucketConfig , $ data , $ type , $ tokenSecret = '' ) {
89109 if (is_array ($ data )) {
90110 ksort ($ data );
91111 $ string = '' ;
0 commit comments