KeyId::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 36
rs 9.584
cc 2
nc 2
nop 5
1
<?php
2
3
namespace Rogierw\RwAcme\Support;
4
5
class KeyId
6
{
7
    public static function generate(
8
        #[\SensitiveParameter] string $accountPrivateKey,
9
        string $kid,
10
        string $url,
11
        string $nonce,
12
        ?array $payload = null
13
    ): array {
14
        $privateKey = openssl_pkey_get_private($accountPrivateKey);
15
16
        $data = [
17
            'alg' => 'RS256',
18
            'kid' => $kid,
19
            'nonce' => $nonce,
20
            'url' => $url,
21
        ];
22
23
        $payload = is_array($payload)
24
            ? str_replace('\\/', '/', json_encode($payload))
25
            : '';
26
27
        $payload64 = Base64::urlSafeEncode($payload);
28
        $protected64 = Base64::urlSafeEncode(json_encode($data));
29
30
        openssl_sign(
31
            $protected64.'.'.$payload64,
32
            $signed,
33
            $privateKey,
34
            'SHA256'
35
        );
36
37
        $signed64 = Base64::urlSafeEncode($signed);
38
39
        return [
40
            'protected' => $protected64,
41
            'payload' => $payload64,
42
            'signature' => $signed64,
43
        ];
44
    }
45
}
46