Skip to content

Commit

Permalink
Add simple method to export a public key
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jun 5, 2016
1 parent 7e99d31 commit dc32c87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/GPGMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dc32c87

Please sign in to comment.