Signature.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace AliyunMNS\Signature;
use AliyunMNS\Requests\BaseRequest;
use AliyunMNS\Constants;
class Signature
{
static public function SignRequest($accessKey, BaseRequest &$request)
{
$canonicalizedMNSHeaders = "";
$headers = $request->getHeaders();
$contentMd5 = "";
if (isset($headers['Content-MD5']))
{
$contentMd5 = $headers['Content-MD5'];
}
$contentType = "";
if (isset($headers['Content-Type']))
{
$contentType = $headers['Content-Type'];
}
$date = $headers['Date'];
$queryString = $request->getQueryString();
$canonicalizedResource = $request->getResourcePath();
if ($queryString != NULL)
{
$canonicalizedResource .= "?" . $request->getQueryString();
}
if (0 !== strpos($canonicalizedResource, "/"))
{
$canonicalizedResource = "/" . $canonicalizedResource;
}
$tmpHeaders = array();
foreach ($headers as $key => $value)
{
if (0 === strpos($key, Constants::MNS_HEADER_PREFIX))
{
$tmpHeaders[$key] = $value;
}
}
ksort($tmpHeaders);
$canonicalizedMNSHeaders = implode("\n", array_map(function ($v, $k) { return $k . ":" . $v; }, $tmpHeaders, array_keys($tmpHeaders)));
$stringToSign = strtoupper($request->getMethod()) . "\n" . $contentMd5 . "\n" . $contentType . "\n" . $date . "\n" . $canonicalizedMNSHeaders . "\n" . $canonicalizedResource;
return base64_encode(hash_hmac("sha1", $stringToSign, $accessKey, $raw_output = TRUE));
}
}
?>