1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace JWX\Util; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class offering Base64 encoding and decoding. |
9
|
|
|
*/ |
10
|
|
|
class Base64 |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Encode a string using base64url variant. |
14
|
|
|
* |
15
|
|
|
* @link https://en.wikipedia.org/wiki/Base64#URL_applications |
16
|
|
|
* @param string $data |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
170 |
|
public static function urlEncode(string $data): string |
20
|
|
|
{ |
21
|
170 |
|
return strtr(rtrim(self::encode($data), "="), "+/", "-_"); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Decode a string using base64url variant. |
26
|
|
|
* |
27
|
|
|
* @link https://en.wikipedia.org/wiki/Base64#URL_applications |
28
|
|
|
* @param string $data |
29
|
|
|
* @throws \UnexpectedValueException |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
130 |
|
public static function urlDecode(string $data): string |
33
|
|
|
{ |
34
|
130 |
|
$data = strtr($data, "-_", "+/"); |
35
|
130 |
|
switch (strlen($data) % 4) { |
36
|
130 |
|
case 0: |
37
|
78 |
|
break; |
38
|
112 |
|
case 2: |
39
|
80 |
|
$data .= "=="; |
40
|
80 |
|
break; |
41
|
68 |
|
case 3: |
42
|
67 |
|
$data .= "="; |
43
|
67 |
|
break; |
44
|
|
|
default: |
45
|
1 |
|
throw new \UnexpectedValueException( |
46
|
1 |
|
"Malformed base64url encoding."); |
47
|
|
|
} |
48
|
129 |
|
return self::decode($data); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check whether string is validly base64url encoded. |
53
|
|
|
* |
54
|
|
|
* @link https://en.wikipedia.org/wiki/Base64#URL_applications |
55
|
|
|
* @param string $data |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
128 |
|
public static function isValidURLEncoding(string $data): bool |
59
|
|
|
{ |
60
|
128 |
|
return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Encode a string in base64. |
65
|
|
|
* |
66
|
|
|
* @link https://tools.ietf.org/html/rfc4648#section-4 |
67
|
|
|
* @param string $data |
68
|
|
|
* @throws \RuntimeException If encoding fails |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
173 |
|
public static function encode(string $data): string |
72
|
|
|
{ |
73
|
173 |
|
$ret = @base64_encode($data); |
74
|
173 |
|
if (!is_string($ret)) { |
75
|
|
|
$err = error_get_last(); |
76
|
|
|
$msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
77
|
|
|
throw new \RuntimeException($msg ?? "base64_encode() failed."); |
78
|
|
|
} |
79
|
173 |
|
return $ret; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Decode a string from base64 encoding. |
84
|
|
|
* |
85
|
|
|
* @link https://tools.ietf.org/html/rfc4648#section-4 |
86
|
|
|
* @param string $data |
87
|
|
|
* @throws \RuntimeException If decoding fails |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
131 |
|
public static function decode(string $data): string |
91
|
|
|
{ |
92
|
131 |
|
$ret = base64_decode($data, true); |
93
|
131 |
|
if (!is_string($ret)) { |
94
|
1 |
|
$err = error_get_last(); |
95
|
1 |
|
$msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
96
|
1 |
|
throw new \RuntimeException($msg ?? "base64_decode() failed."); |
97
|
|
|
} |
98
|
130 |
|
return $ret; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check whether string is validly base64 encoded. |
103
|
|
|
* |
104
|
|
|
* @link https://tools.ietf.org/html/rfc4648#section-4 |
105
|
|
|
* @param string $data |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
6 |
|
public static function isValid(string $data): bool |
109
|
|
|
{ |
110
|
6 |
|
return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|