Skip to content

Commit

Permalink
Remove spaces in full key fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Mar 25, 2015
1 parent 219d3ad commit 40d004d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 39 deletions.
2 changes: 1 addition & 1 deletion framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php
Expand Up @@ -338,7 +338,7 @@ public function getFingerprintsFromKey($pgpdata)
if (preg_match('/pub\s+\w+\/(\w{8})/', $line, $matches)) {
$keyid = '0x' . $matches[1];
} elseif ($keyid && preg_match('/^\s+[\s\w]+=\s*([\w\s]+)$/m', $line, $matches)) {
$fingerprints[$keyid] = trim($matches[1]);
$fingerprints[$keyid] = str_replace(' ', '', $matches[1]);
$keyid = null;
}
}
Expand Down
125 changes: 87 additions & 38 deletions framework/Crypt/test/Horde/Crypt/PgpTest.php
Expand Up @@ -11,9 +11,8 @@

class Horde_Crypt_PgpTest extends PHPUnit_Framework_TestCase
{
protected $_language;
protected $_pgp;
protected $_privkey;
protected $_pubkey;

protected function setUp()
{
Expand All @@ -28,9 +27,6 @@ protected function setUp()
'program' => '/usr/bin/gpg',
'temp' => sys_get_temp_dir()
));

$this->_privkey = file_get_contents(__DIR__ . '/fixtures/pgp_private.asc');
$this->_pubkey = file_get_contents(__DIR__ . '/fixtures/pgp_public.asc');
}

protected function tearDown()
Expand All @@ -53,7 +49,7 @@ public function testBug6601()
E-Mail: rselsky@bu.edu
Hash-Algorithm: pgp-sha1
Key ID: 0xF3C01D42
Key Fingerprint: 5912 D91D 4C79 C670 1FFF 1486 04A6 7B37 F3C0 1D42
Key Fingerprint: 5912D91D4C79C6701FFF148604A67B37F3C01D42
',
$this->_pgp->pgpPrettyKey($data)
Expand All @@ -68,8 +64,8 @@ public function testPgpDecrypt()

$decrypt = $this->_pgp->decrypt($crypt, array(
'passphrase' => 'Secret',
'privkey' => $this->_privkey,
'pubkey' => $this->_pubkey,
'privkey' => $this->getPrivateKey(),
'pubkey' => $this->getPublicKey(),
'type' => 'message'
));

Expand Down Expand Up @@ -129,7 +125,7 @@ public function testPgpEncrypt()
$clear = file_get_contents(__DIR__ . '/fixtures/clear.txt');

$out = $this->_pgp->encrypt($clear, array(
'recips' => array('me@example.com' => $this->_pubkey),
'recips' => array('me@example.com' => $this->getPublicKey()),
'type' => 'message'
));

Expand Down Expand Up @@ -236,7 +232,7 @@ protected function _testParsePGPData($data)

public function testPgpPacketInformation()
{
$out = $this->_pgp->pgpPacketInformation($this->_pubkey);
$out = $this->_pgp->pgpPacketInformation($this->getPublicKey());

$this->assertArrayHasKey(
'public_key',
Expand All @@ -259,7 +255,7 @@ public function testPgpPacketInformation()
$out['keyid']
);

$out = $this->_pgp->pgpPacketInformation($this->_privkey);
$out = $this->_pgp->pgpPacketInformation($this->getPrivateKey());

$this->assertArrayHasKey(
'secret_key',
Expand All @@ -285,21 +281,30 @@ public function testPgpPacketInformation()

public function testPgpPacketSignature()
{
$out = $this->_pgp->pgpPacketSignature($this->_pubkey, 'me@example.com');
$out = $this->_pgp->pgpPacketSignature(
$this->getPublicKey(),
'me@example.com'
);

$this->assertEquals(
'7CA74426BADEABD7',
$out['keyid']
);

$out = $this->_pgp->pgpPacketSignature($this->_privkey, 'me@example.com');
$out = $this->_pgp->pgpPacketSignature(
$this->getPrivateKey(),
'me@example.com'
);

$this->assertEquals(
'7CA74426BADEABD7',
$out['keyid']
);

$out = $this->_pgp->pgpPacketSignature($this->_privkey, 'foo@example.com');
$out = $this->_pgp->pgpPacketSignature(
$this->getPrivateKey(),
'foo@example.com'
);

$this->assertArrayNotHasKey(
'keyid',
Expand All @@ -309,21 +314,30 @@ public function testPgpPacketSignature()

public function testPgpPacketSignatureByUidIndex()
{
$out = $this->_pgp->pgpPacketSignatureByUidIndex($this->_pubkey, 'id1');
$out = $this->_pgp->pgpPacketSignatureByUidIndex(
$this->getPublicKey(),
'id1'
);

$this->assertEquals(
'7CA74426BADEABD7',
$out['keyid']
);

$out = $this->_pgp->pgpPacketSignatureByUidIndex($this->_privkey, 'id1');
$out = $this->_pgp->pgpPacketSignatureByUidIndex(
$this->getPrivateKey(),
'id1'
);

$this->assertEquals(
'7CA74426BADEABD7',
$out['keyid']
);

$out = $this->_pgp->pgpPacketSignatureByUidIndex($this->_privkey, 'id2');
$out = $this->_pgp->pgpPacketSignatureByUidIndex(
$this->getPrivateKey(),
'id2'
);

$this->assertArrayNotHasKey(
'keyid',
Expand All @@ -345,10 +359,10 @@ public function testPgpPrettyKey()
E-Mail: me@example.com
Hash-Algorithm: pgp-sha1
Key ID: 0xBADEABD7
Key Fingerprint: 966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7
Key Fingerprint: 966F4BA9569DE6F65E8253977CA74426BADEABD7
',
$this->_pgp->pgpPrettyKey($this->_pubkey)
$this->_pgp->pgpPrettyKey($this->getPublicKey())
);

$this->assertEquals(
Expand All @@ -361,28 +375,45 @@ public function testPgpPrettyKey()
E-Mail: me@example.com
Hash-Algorithm: pgp-sha1
Key ID: 0xBADEABD7
Key Fingerprint: 966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7
Key Fingerprint: 966F4BA9569DE6F65E8253977CA74426BADEABD7
',
$this->_pgp->pgpPrettyKey($this->_privkey)
$this->_pgp->pgpPrettyKey($this->getPrivateKey())
);
}

public function testPgpGetFingerprintsFromKey()
/**
* @dataProvider pgpGetFingerprintsFromKeyProvider
*/
public function testPgpGetFingerprintsFromKey($expected, $key)
{
$this->assertEquals(
array('0xBADEABD7' => '966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7'),
$this->_pgp->getFingerprintsFromKey($this->_pubkey)
$expected,
$this->_pgp->getFingerprintsFromKey($key)
);
$this->assertEquals(
array('0xBADEABD7' => '966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7'),
$this->_pgp->getFingerprintsFromKey($this->_privkey)
}

public function pgpGetFingerprintsFromKeyProvider()
{
return array(
array(
array(
'0xBADEABD7' => '966F4BA9569DE6F65E8253977CA74426BADEABD7'
),
$this->getPublicKey()
),
array(
array(
'0xBADEABD7' => '966F4BA9569DE6F65E8253977CA74426BADEABD7'
),
$this->getPrivateKey()
)
);
}

public function pgpPublicKeyMIMEPart()
{
$mime_part = $this->_pgp->publicKeyMIMEPart($this->_pubkey);
$mime_part = $this->_pgp->publicKeyMIMEPart($this->getPublicKey());

$this->assertEquals(
'application/pgp-keys',
Expand Down Expand Up @@ -424,8 +455,8 @@ public function testPgpSign()

$out = $this->_pgp->encrypt($clear, array(
'passphrase' => 'Secret',
'privkey' => $this->_privkey,
'pubkey' => $this->_pubkey,
'privkey' => $this->getPrivateKey(),
'pubkey' => $this->getPublicKey(),
'type' => 'signature'
));

Expand All @@ -442,8 +473,8 @@ public function testPgpSign()

$out = $this->_pgp->encrypt($clear, array(
'passphrase' => 'Secret',
'privkey' => $this->_privkey,
'pubkey' => $this->_pubkey,
'privkey' => $this->getPrivateKey(),
'pubkey' => $this->getPublicKey(),
'sigtype' => 'cleartext',
'type' => 'signature'
));
Expand Down Expand Up @@ -484,7 +515,7 @@ public function testDecryptSignature()
$out = $this->_pgp->decrypt(
file_get_contents(__DIR__ . '/fixtures/clear.txt'),
array(
'pubkey' => $this->_pubkey,
'pubkey' => $this->getPublicKey(),
'signature' => file_get_contents(__DIR__ . '/fixtures/pgp_signature.txt'),
'type' => 'detached-signature'
)
Expand All @@ -495,7 +526,7 @@ public function testDecryptSignature()
$out = $this->_pgp->decrypt(
file_get_contents(__DIR__ . '/fixtures/pgp_signed.txt'),
array(
'pubkey' => $this->_pubkey,
'pubkey' => $this->getPublicKey(),
'type' => 'signature'
)
);
Expand All @@ -505,7 +536,7 @@ public function testDecryptSignature()
$out = $this->_pgp->decrypt(
file_get_contents(__DIR__ . '/fixtures/pgp_signed2.txt'),
array(
'pubkey' => $this->_pubkey,
'pubkey' => $this->getPublicKey(),
'type' => 'signature'
)
);
Expand All @@ -516,19 +547,37 @@ public function testDecryptSignature()
public function testVerifyPassphrase()
{
$this->assertTrue(
$this->_pgp->verifyPassphrase($this->_pubkey, $this->_privkey, 'Secret')
$this->_pgp->verifyPassphrase(
$this->getPublicKey(),
$this->getPrivateKey(),
'Secret'
)
);

$this->assertFalse(
$this->_pgp->verifyPassphrase($this->_pubkey, $this->_privkey, 'Wrong')
$this->_pgp->verifyPassphrase(
$this->getPublicKey(),
$this->getPrivateKey(),
'Wrong'
)
);
}

public function testGetPublicKeyFromPrivateKey()
{
$this->assertNotNull(
$this->_pgp->getPublicKeyFromPrivateKey($this->_privkey)
$this->_pgp->getPublicKeyFromPrivateKey($this->getPrivateKey())
);
}

protected function getPrivateKey()
{
return file_get_contents(__DIR__ . '/fixtures/pgp_private.asc');
}

protected function getPublicKey()
{
return file_get_contents(__DIR__ . '/fixtures/pgp_public.asc');
}

}

0 comments on commit 40d004d

Please sign in to comment.