ChallengeAuthorizationKey::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LE_ACME2\Struct;
4
5
use LE_ACME2\Account;
6
use LE_ACME2\Utilities;
7
8
class ChallengeAuthorizationKey {
9
10
    private $_account;
11
12
    public function __construct(Account $account) {
13
        $this->_account = $account;
14
    }
15
16
    public function get(string $token) : string {
17
        return $token . '.' . $this->_getDigest();
18
    }
19
20
    public function getEncoded(string $token) : string {
21
        return Utilities\Base64::UrlSafeEncode(
22
            hash('sha256', $this->get($token), true)
23
        );
24
    }
25
26
    private function _getDigest() : string {
27
28
        $privateKey = openssl_pkey_get_private(file_get_contents($this->_account->getKeyDirectoryPath() . 'private.pem'));
29
        $details = openssl_pkey_get_details($privateKey);
30
31
        $header = array(
32
            "e" => Utilities\Base64::UrlSafeEncode($details["rsa"]["e"]),
33
            "kty" => "RSA",
34
            "n" => Utilities\Base64::UrlSafeEncode($details["rsa"]["n"])
35
36
        );
37
        return Utilities\Base64::UrlSafeEncode(hash('sha256', json_encode($header), true));
38
    }
39
}