3

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();
h3rx
  • 33
  • 1
  • 4
  • Just FYI, the packagist.org URL is wrong. It's https://packagist.org/packages/phpseclib/phpseclib . phpsec/phpsec is another unrelated project. – neubert Feb 26 '15 at 15:31

2 Answers2

10

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

Community
  • 1
  • 1
madbob
  • 456
  • 1
  • 6
  • 13
6

PHPSec does use composer, you should be able to just composer require "phpseclib/phpseclib=~0.3.10" and have it available (autoloaded).

$rsa = new \Crypt_RSA();
crynobone
  • 1,814
  • 12
  • 22
  • That installed the phpseclib successfully, however I still dont know how to use the lib in my code. **MyController.php** now looks like this: `$rsa = Crypt_RSA();` and this is the error I get `Call to undefined function App\Http\Controllers\Crypt_RSA()` – h3rx Feb 26 '15 at 12:03
  • I also tried to Import it at the top of the Controller like this: `use Crypt_RSA;` and calling it like this `$rsa = Crypt_RSA::Crypt_RSA();` but the I get the following error `Non-static method Crypt_RSA::Crypt_RSA() cannot be called statically, assuming $this from incompatible context` this seems to load the lib in the right way. I think this should be the way the lib is meant to be used, it just seems that I am still missing something... – h3rx Feb 26 '15 at 12:06
  • Oh, just a typo :) importing it at the top of the controller with `use Crypt_RSA;` and running `$rsa = new Crypt_RSA();` works like a charm. Thanks for help! – h3rx Feb 26 '15 at 12:12