sop /
jwx
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types = 1); |
||
| 4 | |||
| 5 | namespace Sop\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 | * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
||
| 16 | */ |
||
| 17 | 171 | public static function urlEncode(string $data): string |
|
| 18 | { |
||
| 19 | 171 | return strtr(rtrim(self::encode($data), '='), '+/', '-_'); |
|
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Decode a string using base64url variant. |
||
| 24 | * |
||
| 25 | * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
||
| 26 | * |
||
| 27 | * @throws \UnexpectedValueException |
||
| 28 | */ |
||
| 29 | 131 | public static function urlDecode(string $data): string |
|
| 30 | { |
||
| 31 | 131 | $data = strtr($data, '-_', '+/'); |
|
| 32 | 131 | switch (strlen($data) % 4) { |
|
| 33 | 131 | case 0: |
|
| 34 | 78 | break; |
|
| 35 | 113 | case 2: |
|
| 36 | 81 | $data .= '=='; |
|
| 37 | 81 | break; |
|
| 38 | 68 | case 3: |
|
| 39 | 67 | $data .= '='; |
|
| 40 | 67 | break; |
|
| 41 | default: |
||
| 42 | 1 | throw new \UnexpectedValueException( |
|
| 43 | 1 | 'Malformed base64url encoding.'); |
|
| 44 | } |
||
| 45 | 130 | return self::decode($data); |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Check whether string is validly base64url encoded. |
||
| 50 | * |
||
| 51 | * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
||
| 52 | */ |
||
| 53 | 141 | public static function isValidURLEncoding(string $data): bool |
|
| 54 | { |
||
| 55 | 141 | return 1 === preg_match('#^[A-Za-z0-9\-_]*$#', $data); |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Encode a string in base64. |
||
| 60 | * |
||
| 61 | * @see https://tools.ietf.org/html/rfc4648#section-4 |
||
| 62 | */ |
||
| 63 | 174 | public static function encode(string $data): string |
|
| 64 | { |
||
| 65 | 174 | return base64_encode($data); |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Decode a string from base64 encoding. |
||
| 70 | * |
||
| 71 | * @see https://tools.ietf.org/html/rfc4648#section-4 |
||
| 72 | * |
||
| 73 | * @throws \RuntimeException If decoding fails |
||
| 74 | */ |
||
| 75 | 132 | public static function decode(string $data): string |
|
| 76 | { |
||
| 77 | 132 | $ret = base64_decode($data, true); |
|
| 78 | 132 | if (!is_string($ret)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 79 | 1 | $err = error_get_last(); |
|
| 80 | 1 | $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 81 | 1 | throw new \RuntimeException($msg ?? 'base64_decode() failed.'); |
|
| 82 | } |
||
| 83 | 131 | return $ret; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Check whether string is validly base64 encoded. |
||
| 88 | * |
||
| 89 | * @see https://tools.ietf.org/html/rfc4648#section-4 |
||
| 90 | */ |
||
| 91 | 7 | public static function isValid(string $data): bool |
|
| 92 | { |
||
| 93 | 7 | return 1 === preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data); |
|
| 94 | } |
||
| 95 | } |
||
| 96 |