I found this answer and it helped me half the way: How do you use the PHP OpenPGP library?
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.
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)
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. I crushed my Brain which "use" command I should use and I just have no more ideas.
from composer.json the name is
"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". 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. I am very grateful for any hint to namespaces in Symfony.