1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-25 21:19:21 +01:00
phorge-phorge/src/infrastructure/util/PhabricatorSSHKeyGenerator.php
epriestley 44fc671b3f Add a "Generate Keypair" option on the SSH Keys panel
Summary: Ref T4587. Add an option to automatically generate a keypair, associate the public key, and save the private key.

Test Plan: Generated some keypairs. Hit error conditions, etc.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: aran, epriestley

Maniphest Tasks: T4587

Differential Revision: https://secure.phabricator.com/D8513
2014-03-12 18:17:11 -07:00

32 lines
753 B
PHP

<?php
final class PhabricatorSSHKeyGenerator extends Phobject {
public static function assertCanGenerateKeypair() {
$binary = 'ssh-keygen';
if (!Filesystem::resolveBinary($binary)) {
throw new Exception(
pht(
'Can not generate keys: unable to find "%s" in PATH!',
$binary));
}
}
public static function generateKeypair() {
self::assertCanGenerateKeypair();
$tempfile = new TempFile();
$keyfile = dirname($tempfile).DIRECTORY_SEPARATOR.'keytext';
execx(
'ssh-keygen -t rsa -N %s -f %s',
'',
$keyfile);
$public_key = Filesystem::readFile($keyfile.'.pub');
$private_key = Filesystem::readFile($keyfile);
return array($public_key, $private_key);
}
}