Base64SafeEncoder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 7
c 0
b 0
f 0
dl 0
loc 20
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 3 1
A decode() 0 8 2
A hashEncode() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the LetsEncrypt ACME client.
7
 *
8
 * @author    Ivanov Aleksandr <[email protected]>
9
 * @copyright 2019-2020
10
 * @license   https://github.com/misantron/letsencrypt-client/blob/master/LICENSE MIT License
11
 */
12
13
namespace LetsEncrypt\Helper;
14
15
final class Base64SafeEncoder
16
{
17
    public function encode(string $input): string
18
    {
19
        return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
20
    }
21
22
    public function decode(string $input): string
23
    {
24
        $remainder = strlen($input) % 4;
25
        if ($remainder) {
26
            $input .= str_repeat('=', 4 - $remainder);
27
        }
28
29
        return base64_decode(strtr($input, '-_', '+/'));
30
    }
31
32
    public function hashEncode(string $payload): string
33
    {
34
        return $this->encode(hash('sha256', $payload, true));
35
    }
36
}
37