forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSshKey.php
More file actions
42 lines (33 loc) · 948 Bytes
/
Copy pathSshKey.php
File metadata and controls
42 lines (33 loc) · 948 Bytes
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
<?php
namespace PHPCensor\Helper;
/**
* 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 = ['private_key' => '', 'public_key' => ''];
$output = @shell_exec('ssh-keygen -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@php-censor"');
if (!empty($output)) {
$pub = file_get_contents($keyFile . '.pub');
$prv = file_get_contents($keyFile);
if (!empty($pub)) {
$return['public_key'] = $pub;
}
if (!empty($prv)) {
$return['private_key'] = $prv;
}
}
return $return;
}
}