【问题标题】:Integrate phpseclib into Laravel 5将 phpseclib 集成到 Laravel 5
【发布时间】:2015-02-26 10:29:53
【问题描述】:

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

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

MyController.php

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

这似乎不再起作用了:

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';
}

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

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

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

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

$rsa = new Crypt_RSA();

【问题讨论】:

标签: php laravel phpseclib


【解决方案1】:

重做 this other answer 并查看 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";

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

【讨论】:

  • 对我来说,我使用的是 1.0 版并且我已经设法直接使用 RSA
【解决方案2】:

PHPSec 确实使用了作曲家,您应该可以只使用 composer require "phpseclib/phpseclib=~0.3.10" 并使其可用(自动加载)。

$rsa = new \Crypt_RSA();

【讨论】:

  • 成功安装了phpseclib,但是我仍然不知道如何在我的代码中使用该lib。 MyController.php 现在看起来像这样:$rsa = Crypt_RSA(); 这是我得到的错误Call to undefined function App\Http\Controllers\Crypt_RSA()
  • 我也尝试像这样在控制器顶部导入它:use Crypt_RSA; 并像这样调用它$rsa = Crypt_RSA::Crypt_RSA(); 但我收到以下错误Non-static method Crypt_RSA::Crypt_RSA() cannot be called statically, assuming $this from incompatible context 这似乎加载了库以正确的方式。我认为这应该是 lib 的使用方式,只是似乎我仍然缺少一些东西......
  • 哦,只是一个错字:) 在控制器顶部使用use Crypt_RSA; 导入它并运行$rsa = new Crypt_RSA(); 就像一个魅力。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多