feat: able to make rsa/ed ssh keys

This commit is contained in:
Andras Bacsai
2024-04-03 13:45:49 +02:00
parent 82f74e2264
commit 22a1d3882e
4 changed files with 41 additions and 14 deletions

View File

@@ -39,6 +39,7 @@ use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Token\Builder;
use phpseclib3\Crypt\EC;
use Poliander\Cron\CronExpression;
use Visus\Cuid2\Cuid2;
use phpseclib3\Crypt\RSA;
@@ -165,13 +166,22 @@ function generate_random_name(?string $cuid = null): string
}
return Str::kebab("{$generator->getName()}-$cuid");
}
function generateSSHKey()
function generateSSHKey(string $type = 'rsa')
{
$key = RSA::createKey();
return [
'private' => $key->toString('PKCS1'),
'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key'])
];
if ($type === 'rsa') {
$key = RSA::createKey();
return [
'private' => $key->toString('PKCS1'),
'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key'])
];
} else if ($type === 'ed25519') {
$key = EC::createKey('Ed25519');
return [
'private' => $key->toString('OpenSSH'),
'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key'])
];
}
throw new Exception('Invalid key type');
}
function formatPrivateKey(string $privateKey)
{