-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCrypto.php
More file actions
37 lines (29 loc) · 1.2 KB
/
Crypto.php
File metadata and controls
37 lines (29 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace App\Utils;
final readonly class Crypto
{
public function generateSslCertificate(): string
{
if (!extension_loaded('openssl')) {
throw new \RuntimeException('OpenSSL extension is required.');
}
$privateKey = openssl_pkey_new();
$certSignRequest = openssl_csr_new([], $privateKey);
$certificate = openssl_csr_sign($certSignRequest, null, $privateKey, 1);
$tmpDir = sys_get_temp_dir();
$privateKeyPath = tempnam($tmpDir, 'pkey');
$certificatePath = tempnam($tmpDir, 'cert');
if (!openssl_pkey_export_to_file($privateKey, $privateKeyPath, 'powercloud')) {
throw new \RuntimeException('Private key generation failed: ' . openssl_error_string());
}
if (!openssl_x509_export_to_file($certificate, $certificatePath)) {
throw new \RuntimeException('OpenSSL certificate generation failed: ' . openssl_error_string());
}
register_shutdown_function(static function () use ($privateKeyPath, $certificatePath): void {
unlink($privateKeyPath);
unlink($certificatePath);
});
return $certificatePath;
}
}