KeyGenerationService::createKeyPair()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Service\DKIM;
12
13
use LogicException;
14
15
class KeyGenerationService
16
{
17
    public const DIGEST_ALGORITHM = 'sha256';
18
    private const KEY_LENGTH = 2048;
19
20
    public function extractPublicKey(string $privateKey): string
21
    {
22
        $res = \openssl_pkey_get_private($privateKey);
23
24
        if (false === $res) {
25
            throw new LogicException('Cannot read private key.');
26
        }
27
28
        return \openssl_pkey_get_details($res)['key'];
29
    }
30
31
    public function createKeyPair(): KeyPair
32
    {
33
        $privateKey = '';
34
        $res = \openssl_pkey_new(
35
            [
36
                'digest_alg' => self::DIGEST_ALGORITHM,
37
                'private_key_bits' => self::KEY_LENGTH,
38
                'private_key_type' => \OPENSSL_KEYTYPE_RSA,
39
            ]
40
        );
41
42
        \openssl_pkey_export($res, $privateKey);
43
44
        return new KeyPair(\openssl_pkey_get_details($res)['key'], $privateKey);
45
    }
46
}
47