Integrate phpseclib into Laravel 5

≡放荡痞女 提交于 2019-12-06 08:44:58

问题


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();

回答1:


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();



回答2:


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



来源:https://stackoverflow.com/questions/28739971/integrate-phpseclib-into-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!