Passed
Push — master ( e7e3c4...d0e289 )
by Rogier
01:26
created

CryptRSA   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 10
dl 0
loc 19
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 17 2
1
<?php
2
3
namespace Rogierw\RwAcme\Support;
4
5
use RuntimeException;
6
7
class CryptRSA
8
{
9
    public static function generate(string $directory): void
10
    {
11
        $res = openssl_pkey_new([
12
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
13
            'private_key_bits' => 4096,
14
        ]);
15
16
        if (!openssl_pkey_export($res, $privateKey)) {
17
            throw new RuntimeException('RSA keypair export failed.');
18
        }
19
20
        $details = openssl_pkey_get_details($res);
21
22
        file_put_contents($directory . 'private.pem', $privateKey);
23
        file_put_contents($directory . 'public.pem', $details['key']);
24
25
        openssl_pkey_free($res);
26
    }
27
}
28