forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSshKey.php
More file actions
58 lines (47 loc) · 1.3 KB
/
Copy pathSshKey.php
File metadata and controls
58 lines (47 loc) · 1.3 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
<?php
namespace PHPCensor\Helper;
use PHPCensor\Config;
/**
* Helper class for dealing with SSH keys.
*/
class SshKey
{
/**
* 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 = Config::getInstance()->get('php-censor.ssh.strength', 2048);
$sshComment = Config::getInstance()->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)) {
$pub = file_get_contents($keyFile . '.pub');
$prv = file_get_contents($keyFile);
if (!empty($pub)) {
$return['ssh_public_key'] = $pub;
}
if (!empty($prv)) {
$return['ssh_private_key'] = $prv;
}
}
return $return;
}
}