4096, 'private_key_type' => OPENSSL_KEYTYPE_RSA, ]); if ($resource === false) { throw new \RuntimeException('Failed to generate SSH key pair.'); } openssl_pkey_export($resource, $privateKey); $details = openssl_pkey_get_details($resource); $publicKey = $details['key'] ?? ''; if ($publicKey === '') { throw new \RuntimeException('Failed to export SSH public key.'); } return [ 'public_key' => trim($publicKey)."\n", 'private_key' => $privateKey, ]; } }