Skip to content

Commit

Permalink
Initial implementation of encrypt()
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Apr 13, 2015
1 parent fb12315 commit bb2bca6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
5 changes: 4 additions & 1 deletion framework/Pgp/lib/Horde/Pgp.php
Expand Up @@ -99,7 +99,10 @@ public function encrypt($text, $keys)
'encrypt',
array(
$text,
is_array($keys) ? $keys : array($keys)
array_map(
array('Horde_Pgp_Element_PublicKey', 'create'),
is_array($keys) ? $keys : array($keys)
)
),
Horde_Pgp_Translation::t("Could not PGP encrypt data.")
);
Expand Down
3 changes: 2 additions & 1 deletion framework/Pgp/lib/Horde/Pgp/Backend.php
Expand Up @@ -59,7 +59,8 @@ public function generateKey($opts)
* Encrypts text using PGP public keys.
*
* @param string $text The text to be PGP encrypted.
* @param array $keys The list of public keys to encrypt.
* @param array $keys The list of public keys to encrypt
* (Horde_Pgp_Element_PublicKey objects).
*
* @return Horde_Pgp_Element_Message The encrypted message.
*/
Expand Down
25 changes: 25 additions & 0 deletions framework/Pgp/lib/Horde/Pgp/Backend/Openpgp.php
Expand Up @@ -126,6 +126,31 @@ public function generateKey($opts)
return Horde_Pgp_Element_PrivateKey::createFromData($m);
}

/**
*/
public function encrypt($text, $keys)
{
$p = array();
foreach ($keys as $val) {
foreach ($val->getMessageOb() as $val2) {
if ($val2 instanceof OpenPGP_PublicKeyPacket) {
$p[] = $val2;
break;
}
}
}

/* TODO: Support ElGamal encryption */
$encrypted = OpenPGP_Crypt_Symmetric::encrypt(
$p,
new OpenPGP_Message(array(
new OpenPGP_LiteralDataPacket($text, array('format' => 'u'))
))
);

return Horde_Pgp_Element_Message::createFromData($encrypted);
}

/**
*/
public function encryptSymmetric($text, $passphrase)
Expand Down
29 changes: 18 additions & 11 deletions framework/Pgp/test/Horde/Pgp/Backend/TestBase.php
Expand Up @@ -80,8 +80,25 @@ public function generateKeyProvider()
);
}

public function testEncrypt()
/**
* @dataProvider encryptProvider
*/
public function testEncrypt($key)
{
$result = $this->_pgp->encrypt(
$this->_getFixture('clear.txt'),
$key
);

$this->assertInstanceOf('Horde_Pgp_Element_Message', $result);
}

public function encryptProvider()
{
return array(
array($this->_getFixture('pgp_public.asc')),
array($this->_getFixture('pgp_public_rsa.txt'))
);
}

/**
Expand Down Expand Up @@ -171,14 +188,4 @@ protected function _getFixture($file)
return file_get_contents(dirname(__DIR__) . '/fixtures/' . $file);
}

protected function _getPrivateKey()
{
return $this->_getFixture('pgp_private.asc');
}

protected function _getPublicKey()
{
return $this->_getFixture('pgp_public.asc');
}

}

0 comments on commit bb2bca6

Please sign in to comment.