Integrate phpseclib into Laravel 5

h3rx picture h3rx · Feb 26, 2015 · Viewed 7.4k times · Source

i am currently migrating my Project from Laravel 4 to Laravel 5, i am still a novice user of Laravel and OOP as well but everything went smoothly so far.

However, in my L4 project I use the phpseclib for generating SSH keys which I imported in the following way:

MyController.php

...
include('../app/libs/phpseclib0.3.10/Crypt/RSA.php');
$rsa = new Crypt_RSA();
...

which does not seems to work anymore:

syntax error, unexpected 'if' (T_IF), expecting identifier (T_STRING) 

.../app/libs/phpseclib0.3.10/Crypt/RSA.php

/**
* Include Crypt_Random
*/
// the class_exists() will only be called if the crypt_random_string function hasn't been defined and
// will trigger a call to __autoload() if you're wanting to auto-load classes
// call function_exists() a second time to stop the include_once from being called outside
// of the auto loader
if (!function_exists('crypt_random_string')) {
include_once 'Random.php';
}

I know that there is a phpseclib package: https://packagist.org/packages/phpseclib/phpseclib

and I also know how to Install it in my L5 Project.

However, I dont know how to use the phpseclib package in my Laravel 5 project after I installed it.

So, after I installed the phpseclib package in my L5 Project, how to create my SSH Key similar to this:

$rsa = new Crypt_RSA();

Answer

madbob picture madbob · Aug 15, 2015

Reworking this other answer and getting a look into the vendor/phpseclib/ folder, I've managed to get it working with this syntax:

use phpseclib\Crypt\RSA;
...
...
$rsa = new RSA();
$rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_OPENSSH);
extract($rsa->createKey());
echo "$publickey\r\n\r\n$privatekey";

At least, it works on phpseclib version ^2.0