src/BaiduBce/Auth/BceV1Signer.php
public function sign(
        array $credentials,
        $httpMethod,
        $path,
        $headers,
        $params,
        $options = array()
    ) {
        if (!isset($options[SignOptions::EXPIRATION_IN_SECONDS])) {
            $expirationInSeconds = SignOptions::DEFAULT_EXPIRATION_IN_SECONDS;
        } else {
            $expirationInSeconds = $options[SignOptions::EXPIRATION_IN_SECONDS];
        }

        // to compatible with ak/sk or accessKeyId/secretAccessKey
        if(isset($credentials['ak'])){
            $accessKeyId = $credentials['ak'];
        }
        if(isset($credentials['sk'])){
            $secretAccessKey = $credentials['sk'];
        }
        if(isset($credentials['accessKeyId'])){
            $accessKeyId = $credentials['accessKeyId'];
        }
        if(isset($credentials['secretAccessKey'])){
            $secretAccessKey = $credentials['secretAccessKey'];
        }

        if (isset($options[SignOptions::TIMESTAMP])) {
            $timestamp = $options[SignOptions::TIMESTAMP];
        } else {
            $timestamp = new \DateTime();
        }
        $timestamp->setTimezone(DateUtils::$UTC_TIMEZONE);

        $authString = BceV1Signer::BCE_AUTH_VERSION . '/' . $accessKeyId . '/'
            . DateUtils::formatAlternateIso8601Date(
                $timestamp
            ) . '/' . $expirationInSeconds;

        $signingKey = hash_hmac('sha256', $authString, $secretAccessKey);

        // Formatting the URL with signing protocol.
        $canonicalURI = BceV1Signer::getCanonicalURIPath($path);
        // Formatting the query string with signing protocol.
        $canonicalQueryString = HttpUtils::getCanonicalQueryString(
            $params,
            true
        );
        // Sorted the headers should be signed from the request.
        $headersToSign = null;
        if (isset($options[SignOptions::HEADERS_TO_SIGN])) {
            $headersToSign = $options[SignOptions::HEADERS_TO_SIGN];
        }
        // Formatting the headers from the request based on signing protocol.
        $canonicalHeader = BceV1Signer::getCanonicalHeaders(
            BceV1Signer::getHeadersToSign($headers, $headersToSign)
        );
        $signedHeaders = '';
        if ($headersToSign !== null) {
            $signedHeaders = strtolower(
                trim(implode(";", array_keys($headersToSign)))
            );
        }
        $canonicalRequest = "$httpMethod\n$canonicalURI\n"
            . "$canonicalQueryString\n$canonicalHeader";

        // Signing the canonical request using key with sha-256 algorithm.
        $signature = hash_hmac('sha256', $canonicalRequest, $signingKey);

        $authorizationHeader = "$authString/$signedHeaders/$signature";

        return $authorizationHeader;
    }
public function sign(
        array $credentials,
        $httpMethod,
        $path,
        $headers,
        $params,
        $options = array()
    ) {
        if (!isset($options[SignOptions::EXPIRATION_IN_SECONDS])) {
            $expirationInSeconds = SignOptions::DEFAULT_EXPIRATION_IN_SECONDS;
        } else {
            $expirationInSeconds = $options[SignOptions::EXPIRATION_IN_SECONDS];
        }

        // to compatible with ak/sk or accessKeyId/secretAccessKey
        if(isset($credentials['ak'])){
            $accessKeyId = $credentials['ak'];
        }
        if(isset($credentials['sk'])){
            $secretAccessKey = $credentials['sk'];
        }
        if(isset($credentials['accessKeyId'])){
            $accessKeyId = $credentials['accessKeyId'];
        }
        if(isset($credentials['secretAccessKey'])){
            $secretAccessKey = $credentials['secretAccessKey'];
        }

        if (isset($options[SignOptions::TIMESTAMP])) {
            $timestamp = $options[SignOptions::TIMESTAMP];
        } else {
            $timestamp = new \DateTime();
        }
        $timestamp->setTimezone(DateUtils::$UTC_TIMEZONE);

        $authString = BceV1Signer::BCE_AUTH_VERSION . '/' . $accessKeyId . '/'
            . DateUtils::formatAlternateIso8601Date(
                $timestamp
            ) . '/' . $expirationInSeconds;

        $signingKey = hash_hmac('sha256', $authString, $secretAccessKey);

        // Formatting the URL with signing protocol.
        $canonicalURI = BceV1Signer::getCanonicalURIPath($path);
        // Formatting the query string with signing protocol.
        $canonicalQueryString = HttpUtils::getCanonicalQueryString(
            $params,
            true
        );
        // Sorted the headers should be signed from the request.
        $headersToSign = null;
        if (isset($options[SignOptions::HEADERS_TO_SIGN])) {
            $headersToSign = $options[SignOptions::HEADERS_TO_SIGN];
        }
        // Formatting the headers from the request based on signing protocol.
        $canonicalHeader = BceV1Signer::getCanonicalHeaders(
            BceV1Signer::getHeadersToSign($headers, $headersToSign)
        );
        $signedHeaders = '';
        if ($headersToSign !== null) {
            $signedHeaders = strtolower(
                trim(implode(";", array_keys($headersToSign)))
            );
        }
        $canonicalRequest = "$httpMethod\n$canonicalURI\n"
            . "$canonicalQueryString\n$canonicalHeader";

        // Signing the canonical request using key with sha-256 algorithm.
        $signature = hash_hmac('sha256', $canonicalRequest, $signingKey);

        $authorizationHeader = "$authString/$signedHeaders/$signature";

        return $authorizationHeader;
    }
public function sign ( array $ credentials , $ httpMethod , $ path , $ headers , $ params , $ options = array ( ) ) { if ( ! isset ( $ options [ SignOptions :: EXPIRATION_IN_SECONDS ] ) ) { $ expirationInSeconds = SignOptions :: DEFAULT_EXPIRATION_IN_SECONDS ; } else { $ expirationInSeconds = $ options [ SignOptions :: EXPIRATION_IN_SECONDS ] ; } // to compatible with ak/sk or accessKeyId/secretAccessKey if ( isset ( $ credentials [ 'ak' ] ) ) { $ accessKeyId = $ credentials [ 'ak' ] ; } if ( isset ( $ credentials [ 'sk' ] ) ) { $ secretAccessKey = $ credentials [ 'sk' ] ; } if ( isset ( $ credentials [ 'accessKeyId' ] ) ) { $ accessKeyId = $ credentials [ 'accessKeyId' ] ; } if ( isset ( $ credentials [ 'secretAccessKey' ] ) ) { $ secretAccessKey = $ credentials [ 'secretAccessKey' ] ; } if ( isset ( $ options [ SignOptions :: TIMESTAMP ] ) ) { $ timestamp = $ options [ SignOptions :: TIMESTAMP ] ; } else { $ timestamp = new \ DateTime ( ) ; } $ timestamp -> setTimezone ( DateUtils :: $ UTC_TIMEZONE ) ; $ authString = BceV1Signer :: BCE_AUTH_VERSION . '/' . $ accessKeyId . '/' . DateUtils :: formatAlternateIso8601Date ( $ timestamp ) . '/' . $ expirationInSeconds ; $ signingKey = hash_hmac ( 'sha256' , $ authString , $ secretAccessKey ) ; // Formatting the URL with signing protocol. $ canonicalURI = BceV1Signer :: getCanonicalURIPath ( $ path ) ; // Formatting the query string with signing protocol. $ canonicalQueryString = HttpUtils :: getCanonicalQueryString ( $ params , true ) ; // Sorted the headers should be signed from the request. $ headersToSign = null ; if ( isset ( $ options [ SignOptions :: HEADERS_TO_SIGN ] ) ) { $ headersToSign = $ options [ SignOptions :: HEADERS_TO_SIGN ] ; } // Formatting the headers from the request based on signing protocol. $ canonicalHeader = BceV1Signer :: getCanonicalHeaders ( BceV1Signer :: getHeadersToSign ( $ headers , $ headersToSign ) ) ; $ signedHeaders = '' ; if ( $ headersToSign !== null ) { $ signedHeaders = strtolower ( trim ( implode ( ";" , array_keys ( $ headersToSign ) ) ) ) ; } $ canonicalRequest = "$httpMethod\n$canonicalURI\n" . "$canonicalQueryString\n$canonicalHeader" ; // Signing the canonical request using key with sha-256 algorithm. $ signature = hash_hmac ( 'sha256' , $ canonicalRequest , $ signingKey ) ; $ authorizationHeader = "$authString/$signedHeaders/$signature" ; return $ authorizationHeader ; }
Sign the given request with the given set of credentials. Modifies the passed-in request to apply the signature.

@param $credentials array the credentials to sign the request with.
@param $httpMethod string
@param $path string
@param $headers array
@param $params array
@param $options  array   the options for signing.
@return string The signed authorization string.
Sign the given request with the given set of credentials . Modifies the passed - in request to apply the signature .