-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathSshKey.php
More file actions
70 lines (57 loc) · 1.67 KB
/
Copy pathSshKey.php
File metadata and controls
70 lines (57 loc) · 1.67 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace PHPCensor\Helper;
use PHPCensor\Common\Application\ConfigurationInterface;
/**
* Helper class for dealing with SSH keys.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class SshKey
{
protected ConfigurationInterface $configuration;
public function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}
/**
* Uses ssh-keygen to generate a public/private key pair.
*
* @return array
*/
public function generate()
{
$tempPath = \sys_get_temp_dir() . '/';
$keyFile = $tempPath . \md5(\microtime(true));
if (!\is_dir($tempPath)) {
\mkdir($tempPath);
}
$return = [
'ssh_private_key' => '',
'ssh_public_key' => ''
];
$sshStrength = $this->configuration->get('php-censor.ssh.strength', 2048);
$sshComment = $this->configuration->get('php-censor.ssh.comment', 'admin@php-censor');
$output = @\shell_exec(
\sprintf(
'ssh-keygen -t rsa -b %s -f %s -N "" -C "%s"',
$sshStrength,
$keyFile,
$sshComment
)
);
if (!empty($output)) {
$publicKey = \file_get_contents($keyFile . '.pub');
$privateKey = \file_get_contents($keyFile);
if (!empty($publicKey)) {
$return['ssh_public_key'] = $publicKey;
}
if (!empty($privateKey)) {
$return['ssh_private_key'] = $privateKey;
}
}
return $return;
}
}