-
Notifications
You must be signed in to change notification settings - Fork 48
/
GeneratePrivateKey.php
58 lines (48 loc) · 1.49 KB
/
GeneratePrivateKey.php
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 Imbo\CliCommand;
use Imbo\Exception\RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GeneratePrivateKey extends Command {
/**
* How many times to try to generate before we give up
*
* @var int
*/
public $maxTries = 10;
/**
* {@inheritdoc}
*/
public function __construct() {
parent::__construct('generate-private-key');
$this->setDescription('Generate a private key');
$this->setHelp('Generate a private key that you can use in the auth configuration');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) : int {
$output->writeln($this->generate());
return self::SUCCESS;
}
/**
* Generate a private key
*
* @return string
*/
public function generate() {
$strong = false;
$i = 0;
while (!$strong && $this->maxTries > $i++) {
$data = openssl_random_pseudo_bytes(32, $strong);
}
if (!$strong) {
throw new RuntimeException('Could not generate private key');
}
// base64_encode to get a decent ascii compatible format, and trim ending ='s.
$key = rtrim(base64_encode($data), '=');
// We change +/ into -_ to avoid any human confusion with paths
return strtr($key, '+/', '-_');
}
}