2017-01-04 11:41:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shaarli;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL-safe Base64 operations
|
|
|
|
*
|
|
|
|
* @see https://en.wikipedia.org/wiki/Base64#URL_applications
|
|
|
|
*/
|
|
|
|
class Base64Url
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Base64Url-encodes data
|
|
|
|
*
|
|
|
|
* @param string $data Data to encode
|
|
|
|
*
|
|
|
|
* @return string Base64Url-encoded data
|
|
|
|
*/
|
2018-10-13 00:19:03 +02:00
|
|
|
public static function encode($data)
|
|
|
|
{
|
2017-01-04 11:41:05 +01:00
|
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes Base64Url-encoded data
|
|
|
|
*
|
|
|
|
* @param string $data Data to decode
|
|
|
|
*
|
|
|
|
* @return string Decoded data
|
|
|
|
*/
|
2018-10-13 00:19:03 +02:00
|
|
|
public static function decode($data)
|
|
|
|
{
|
2017-01-04 11:41:05 +01:00
|
|
|
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
|
|
|
|
}
|
|
|
|
}
|