Skip to content

Commit d2b5ec9

Browse files
committed
Add utility class to Base64 en/decode in a URL safe fashion.
1 parent ac91607 commit d2b5ec9

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/Utils/Base64Url.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Pdsinterop\Solid\Auth\Utils;
4+
5+
/**
6+
* URL-safe Base64 encode and decode
7+
*
8+
* ...as PHP does not natively offer this functionality
9+
*/
10+
class Base64Url
11+
{
12+
private const URL_UNSAFE = '+/';
13+
private const URL_SAFE = '-_';
14+
15+
public static function encode($subject) : string
16+
{
17+
return strtr(rtrim(base64_encode($subject), '='), self::URL_UNSAFE, self::URL_SAFE);
18+
}
19+
20+
public static function decode($subject) : string
21+
{
22+
return base64_decode(strtr($subject, self::URL_SAFE, self::URL_UNSAFE));
23+
}
24+
}

0 commit comments

Comments
 (0)