[英]Using openpgp-php with the Symfony framework Attempted to load class “OpenPGP_SecretKeyPacket” from namespace “App\Controller”
I found this answer and it helped me half the way: How do you use the PHP OpenPGP library?我找到了这个答案,它帮助了我一半: 你如何使用 PHP OpenPGP 库?
First of all, due to dependencies I really failed to set it up properly by just downloading it.首先,由于依赖关系,我真的无法通过下载它来正确设置它。 But as I have Symfony running and installed with composer, I finally got pgp installed (but not working) in Symfony by running composer require singpolyma/openpgp-php
which installed it and the dependencies into the vendor folder.但是因为我有 Symfony 运行并与 composer 一起安装,我终于通过运行composer require singpolyma/openpgp-php
在 Symfony 中安装了 pgp(但不工作),它将它和依赖项安装到供应商文件夹中。
I can use pgp in a standalone php-file if I requires as follows, but this does not work in the controller (even if I add the requires it does not fail more or less than without)如果我需要如下要求,我可以在独立的 php 文件中使用 pgp,但这在 controller 中不起作用(即使我添加了要求,它也不会或多或少地失败)
require("../vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA.php");
require("../vendor/phpseclib/phpseclib/phpseclib/Crypt/Hash.php");
require("../vendor/phpseclib/phpseclib/phpseclib/Math/BigInteger.php");
require("../vendor/singpolyma/openpgp-php/lib/openpgp_crypt_rsa.php");
In the AbstractController of Symfony it does not work that way.在 Symfony 的 AbstractController 中,它不能那样工作。 I crushed my Brain which "use" command I should use and I just have no more ideas.我压碎了我应该使用哪个“使用”命令的大脑,我只是没有更多的想法。
from composer.json the name is来自 composer.json 名字是
"name": "singpolyma/openpgp-php",
but a minus is not a valid name in a namespace.但减号不是命名空间中的有效名称。
I usually get the error我通常会收到错误
Attempted to load class "OpenPGP_SecretKeyPacket" from namespace "App\Controller".尝试从命名空间“App\Controller”加载 class“OpenPGP_SecretKeyPacket”。 Did you forget a "use" statement for another namespace?您是否忘记了另一个名称空间的“使用”语句?
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
class PageApiController extends AbstractController
{
/**
@Route("/ApiTest", methods={"GET"})
*/
public function ApiTest(EntityManagerInterface $entityManager)
{
$rsa = new \phpseclib\Crypt\RSA(); // HERE comes the ERROR
$k = $rsa->createKey(512);
$rsa->loadKey($k['privatekey']);
$nkey = new OpenPGP_SecretKeyPacket(array(
'n' => $rsa->modulus->toBytes(),
'e' => $rsa->publicExponent->toBytes(),
'd' => $rsa->exponent->toBytes(),
'p' => $rsa->primes[2]->toBytes(),
'q' => $rsa->primes[1]->toBytes(),
'u' => $rsa->coefficients[2]->toBytes()
));
$uid = new OpenPGP_UserIDPacket('Test <test@example.com>');
$wkey = new OpenPGP_Crypt_RSA($nkey);
$m = $wkey->sign_key_userid(array($nkey, $uid));
// Serialize private key
$Data = $m->to_bytes();
return $this->json(['Test' => $Data]);
}
}
I must admit I am not used to namespaces in php and I know I do not really understand what is going on in symfony yet.我必须承认我不习惯 php 中的命名空间,而且我知道我还不太了解 symfony 中发生了什么。 I am very grateful for any hint to namespaces in Symfony.我非常感谢 Symfony 中命名空间的任何提示。
There are actually two libraries involved here.这里实际上涉及两个库。 The phpseclib is namespaced so things like the RSA class can be used with a simple new RSA(). phpseclib 是命名空间的,所以像 RSA class 这样的东西可以与一个简单的 new RSA() 一起使用。 However, the OpenPGP stuff is not namespaced and does not seem to support classic autoloading.但是,OpenPGP 的东西没有命名空间,而且似乎不支持经典的自动加载。 Personally I would look for another more up to date library however you can use composer.json files capability to load the necessary include files.我个人会寻找另一个更新的库,但是您可以使用 composer.json 文件功能来加载必要的包含文件。 At which point you can create the secret key packet class cleanly.此时,您可以干净地创建密钥数据包 class。 Which is as far as I went with testing.就我的测试而言。
# create a new project
symfony new --full pgp
cd pgp
composer require singpolyma/openpgp-php
# Edit composer.json
"autoload": {
"psr-4": {
"App\\": "src/"
},
"files": [
"vendor/singpolyma/openpgp-php/lib/openpgp.php",
"vendor/singpolyma/openpgp-php/lib/openpgp_crypt_rsa.php"
]
},
# refresh autoload.php
composer dump-autoload
# Add a test command
namespace App\Command;
use phpseclib\Crypt\RSA;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PgpCommand extends Command
{
protected static $defaultName = 'pgp:test';
protected function execute(InputInterface $input, OutputInterface $output)
{
$rsa = new RSA();
$k = $rsa->createKey(512);
$rsa->loadKey($k['privatekey']);
// Note the leading back slash
$nkey = new \OpenPGP_SecretKeyPacket(array(
'n' => $rsa->modulus->toBytes(),
'e' => $rsa->publicExponent->toBytes(),
'd' => $rsa->exponent->toBytes(),
'p' => $rsa->primes[2]->toBytes(),
'q' => $rsa->primes[1]->toBytes(),
'u' => $rsa->coefficients[2]->toBytes()
));
return Command::SUCCESS;
}
}
# and test
bin/console pgp:test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.