-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.php
More file actions
114 lines (105 loc) · 2.84 KB
/
Copy pathalgorithms.php
File metadata and controls
114 lines (105 loc) · 2.84 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
function generateClientToken($params) {
$md5 = md5($params);
$tmp = substr($md5, 0, 8) . '-' . substr($md5, 8, 4) . '-'
. substr($md5, 12, 4) . '-'
. substr($md5, 16, 4) . '-' . substr($md5, 20, 12);
return $tmp;
}
function generateEipName($length = 6) {
require_once('chars.php');
$keys = array_rand(CharsSet::$LOWER_CHARS, $length);
$word = '';
for ($i = 0; $i < $length; $i++) {
$word .= CharsSet::$LOWER_CHARS[$keys[$i]];
}
return "e-{$word}";
}
function generateAuthorization(
$accessId,
$secretKey,
$timeStamp,
$path,
$params,
$httpMethod,
$headers,
$headerToSign
) {
$authString = generateAuthString($accessId, $timeStamp);
$signature = generateSignature($authString, $secretKey, $path,
$params, $httpMethod, $headers, $headerToSign);
$authorization = "{$authString}/";
if ($headerToSign != NULL){
sort($headerToSign);
$authorization .= join(';', $headerToSign);
}
$authorization.= "/{$signature}";
return $authorization;
}
function generateSignature(
$authString,
$secretKey,
$path,
$params,
$httpMethod,
$headers,
$headerToSign
) {
$signKey = generateSignKey($authString, $secretKey);
$path = normalizeString($path);
$queryString = generateQueryString($params);
$headerString = generateHeaderString($headers, $headerToSign);
$stringToSign = $httpMethod . "\n" . $path . "\n" . $queryString . "\n"
. $headerString;
$signature = hash_hmac('SHA256', $stringToSign, $signKey);
return $signature;
}
function generateSignKey($secretKey, $authString) {
return hash_hmac('SHA256', $secretKey, $authString);
}
function generateAuthString($accessId, $timeStamp) {
return "bce-auth-v1/{$accessId}/" . getCanonicalTime($timeStamp)
. '/1800';
}
function getCanonicalTime($time = -1) {
return date('Y-m-d\TH:i:s\Z', $time === -1 ? time() : $time);
}
function normalizeString($str, $encodingSlash = FALSE) {
$tmp = urlencode($str);
if ($encodingSlash === FALSE) {
$tmp = str_replace('%2F', '/', $tmp);
}
return $tmp;
}
function generateQueryString($params) {
ksort($params);
$tmp = '';
foreach ($params as $key => $value) {
if(gettype($key) === 'integer'){
$tmp .= "{$value}=&";
}else{
$tmp .= "{$key}=" . normalizeString($value, TRUE) .'&';
}
}
return substr($tmp, 0, strlen($tmp) - 1);
}
function generateHeaderString($headers, $headersToSign) {
ksort($headers);
$headersToSign = !isset($headersToSign) ? [
'host',
'content-md5',
'content-length',
'content-type',
] : $headersToSign;
$tmp = '';
foreach ($headers as $key => $value) {
if (in_array($key, $headersToSign, TRUE) or strpos($key,
'x-bce-') === 0) {
$tmp .= normalizeString(strtolower($key)) . ':' .
normalizeString($value,
TRUE)
. "\n";
}
}
return substr($tmp, 0, strlen($tmp) - 1);
}