Completed
Pull Request — master (#267)
by
unknown
11:00
created

Utils::base64UrlEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace QiniuRtc;
3
4
final class Utils
5
{
6
    public static function base64UrlEncode($str)
7
    {
8
        $find = array('+', '/');
9
        $replace = array('-', '_');
10
        return str_replace($find, $replace, base64_encode($str));
11
    }
12
13
    public static function base64UrlDecode($str)
14
    {
15
        $find = array('-', '_');
16
        $replace = array('+', '/');
17
        return base64_decode(str_replace($find, $replace, $str));
18
    }
19
20
    public static function digest($secret, $data)
21
    {
22
        return hash_hmac('sha1', $data, $secret, true);
23
    }
24
25
    public static function sign($secret, $data)
26
    {
27
        return self::base64UrlEncode(self::digest($secret, $data));
28
    }
29
30
    public static function getUserAgent($sdkName, $sdkVersion)
31
    {
32
        $ua = sprintf("%s/%s", $sdkName, $sdkVersion);
33
        if (extension_loaded('curl')) {
34
            $curlVersion = curl_version();
35
            $ua .= ' curl/' . $curlVersion['version'];
36
        }
37
        $ua .= ' PHP/' . PHP_VERSION;
38
        return $ua;
39
    }
40
}
41