aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/http/Base64Url.php
blob: 33fa7c1f8c8f3be693a4cbed58bf293be8277ae1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace Shaarli\Http;

/**
 * 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
     */
    public static function encode($data)
    {
        return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
    }

    /**
     * Decodes Base64Url-encoded data
     *
     * @param string $data Data to decode
     *
     * @return string Decoded data
     */
    public static function decode($data)
    {
        return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
    }
}