phar-io/gnupg
PHP wrapper for GnuPG used by phar-io tools, providing a simple API to verify and manage PGP signatures in PHP. Helps integrate GPG key handling and signature checks into builds and distribution workflows.
Installation
composer require phar-io/gnupg
Ensure the gnupg binary is installed on your system (Linux: sudo apt-get install gnupg, macOS: brew install gnupg).
Basic Initialization
use PharIo\Gnupg\Gnupg;
$gpg = new Gnupg([
'binary' => '/usr/bin/gpg', // Path to GPG binary (adjust if needed)
'options' => [
Gnupg::OPT_NO_VERIFY,
Gnupg::OPT_NO_DEFAULT_KEYRING,
],
]);
First Use Case: Encrypting a Message
$recipientKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
$gpg->addKey($recipientKey);
$encrypted = $gpg->encrypt('Hello, world!');
echo $encrypted; // Outputs armored PGP message
Importing Keys
$gpg->addKey(file_get_contents('public.key'));
$gpg->addSecretKey(file_get_contents('private.key'), 'passphrase');
Tip: Use Laravel’s Storage facade to fetch keys from disk or cloud storage.
Signing and Verifying
// Sign
$signed = $gpg->sign('message', 'private-key-id');
// Verify
$verified = $gpg->verify($signed);
Service Provider Binding
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Gnupg::class, function ($app) {
return new Gnupg([
'binary' => config('gnupg.binary'),
'options' => [Gnupg::OPT_ARMOR],
]);
});
}
Configurable GPG Options
// config/gnupg.php
return [
'binary' => env('GPG_BINARY', '/usr/bin/gpg'),
'options' => [
Gnupg::OPT_ARMOR,
Gnupg::OPT_NO_VERIFY,
],
];
Email Encryption
Use with Laravel’s Mail facade to encrypt email bodies before sending.
$encrypted = $gpg->encrypt($emailBody);
Mail::send([...], function ($message) use ($encrypted) {
$message->setBody($encrypted);
});
Secure API Responses Encrypt sensitive data in API responses using middleware.
// app/Http/Middleware/EncryptResponse.php
public function handle($request, Closure $next)
{
$response = $next($request);
if ($response->isJson() && $request->hasValidToken()) {
$encrypted = $gpg->encrypt($response->getContent());
$response->setContent($encrypted);
}
return $response;
}
Binary Path Issues
GnuPGException if the GPG binary isn’t found.which gpg to locate it dynamically.
$binaryPath = shell_exec('which gpg');
$gpg = new Gnupg(['binary' => trim($binaryPath)]);
Keyring Permissions
~/.gnupg/ or specify a custom keyring.
$gpg = new Gnupg([
'options' => [Gnupg::OPT_KEYRING => '/custom/path/keyring.gpg'],
]);
Passphrase Handling
env() or a secrets manager (e.g., AWS Secrets Manager).
$gpg->addSecretKey($privateKey, env('GPG_PRIVATE_KEY_PASSPHRASE'));
Enable Verbose Output
$gpg = new Gnupg(['options' => [Gnupg::OPT_VERBOSE]]);
Check logs for GPG command execution details.
Validate Armored Output
Ensure Gnupg::OPT_ARMOR is set when working with ASCII-armored messages (e.g., for email).
Custom GPG Commands
Use the exec() method for unsupported operations:
$output = $gpg->exec('list-keys');
Event Listeners Extend the class to log operations or trigger events:
$gpg = new class extends Gnupg {
public function encrypt($message, $recipient) {
\Log::debug("Encrypting for: $recipient");
return parent::encrypt($message, $recipient);
}
};
Fallback for Missing Binary
Implement a fallback (e.g., PHP-GPG) if gnupg is unavailable:
try {
$gpg = new Gnupg([...]);
} catch (\PharIo\Gnupg\Exception\GnuPGException $e) {
$gpg = new FallbackGpgWrapper();
}
How can I help you explore Laravel packages today?