将 phpseclib 集成到 Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28739971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:01:46  来源:igfitidea点击:

Integrate phpseclib into Laravel 5

phplaravelphpseclib

提问by h3rx

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.

我目前正在将我的项目从 Laravel 4 迁移到 Laravel 5,我仍然是 Laravel 和 OOP 的新手用户,但到目前为止一切都很顺利。

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

但是,在我的 L4 项目中,我使用 phpseclib 来生成我通过以下方式导入的 SSH 密钥:

MyController.php

我的控制器.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

.../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

我知道有一个 phpseclib 包:https://packagist.org/packages/phpseclib/phpseclib

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

我也知道如何在我的 L5 项目中安装它。

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

但是,我不知道如何在安装后在我的 Laravel 5 项目中使用 phpseclib 包。

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

因此,在我的 L5 项目中安装了 phpseclib 包后,如何创建类似于此的 SSH 密钥:

$rsa = new Crypt_RSA();

回答by madbob

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

重新编写这个其他答案并查看 vendor/phpseclib/ 文件夹,我设法让它使用以下语法:

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

至少,它适用于 phpseclib 版本 ^2.0

回答by crynobone

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

PHPSec 确实使用作曲家,您应该能够composer require "phpseclib/phpseclib=~0.3.10"让它可用(自动加载)。

$rsa = new \Crypt_RSA();