Skip to content

Commit

Permalink
Merge pull request #342 from matslindh/generate-shorter-private-keys
Browse files Browse the repository at this point in the history
Generate private keys with base64-encoded raw random bytes
  • Loading branch information
rexxars committed Aug 13, 2015
2 parents f5360d0 + 2606f06 commit 882b847
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions library/ImboCli/Command/GeneratePrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$i = 0;

while (!$strong && $this->maxTries > $i++) {
$data = openssl_random_pseudo_bytes(64, $strong);
$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
$key = strtr($key, '+/', '-_');

$output->writeln(hash('sha256', $data));
$output->writeln($key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testCanGenerateAPrivateKey() {
$commandTester = new CommandTester($this->command);
$commandTester->execute(array('command' => $this->command->getName()));

$this->assertRegExp('/^[a-f0-9]{64}$/', trim($commandTester->getDisplay()));
$this->assertRegExp('/^[a-zA-Z_\\-0-9]{8,}$/', trim($commandTester->getDisplay()));
}

/**
Expand Down

0 comments on commit 882b847

Please sign in to comment.