diff --git a/src/GPGMailer.php b/src/GPGMailer.php index f467b55..77c4083 100644 --- a/src/GPGMailer.php +++ b/src/GPGMailer.php @@ -37,13 +37,27 @@ public function __construct( } /** + * Get the public key corresponding to a fingerprint. + * + * @param string $fingerprint + * @return string + */ + public function export(string $fingerprint): string + { + $gnupg = new \Crypt_GPG($this->options); + $gnupg->addEncryptKey($fingerprint); + return $gnupg->exportPublicKey($fingerprint, true); + } + + /** + * Encrypt the body of an email. + * * @param Message $message * @param string $fingerprint * @return Message */ public function encrypt(Message $message, string $fingerprint): Message { - $gnupg = new \Crypt_GPG($this->options); $gnupg->addEncryptKey($fingerprint); diff --git a/tests/EmailTest.php b/tests/EmailTest.php index 8995930..a3c3978 100644 --- a/tests/EmailTest.php +++ b/tests/EmailTest.php @@ -25,7 +25,7 @@ public function testEncryptedMessage() ); $publicKey = '-----BEGIN PGP PUBLIC KEY BLOCK----- -Version: SKS 1.1.5 +Version: GnuPG v1 mQENBFUgwRUBCADcIpqNwyYc5UmY/tpx1sF/rQ3knR1YNXYZThzFV+Gmqhp1fDH5qBs9foh1 xwI6O7knWmQngnf/nBumI3x6xj7PuOdEZUh2FwCG/VWnglW8rKmoHzHAivjiu9SLnPIPAgHS @@ -63,6 +63,11 @@ public function testEncryptedMessage() -----END PGP PUBLIC KEY BLOCK-----'; $fingerprint = $gpgMailer->import($publicKey); + $exported = $gpgMailer->export($fingerprint); + $this->assertSame( + \str_replace(["\r", "\n"], '', $publicKey), + \str_replace(["\r", "\n"], '', $exported) + ); $this->assertSame( '7F52D5C61D1255C731362E826B97A1C2826404DA', $fingerprint