Skip to content

Commit

Permalink
Derive public keys from secret keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Dec 30, 2015
1 parent 4531fd0 commit 4f90faa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Asymmetric/EncryptionSecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ public function __construct($keyMaterial = '', ...$args)
{
parent::__construct($keyMaterial, false);
}

/**
* See the appropriate derived class.
*
* @return SignaturePublicKey
*/
public function derivePublicKey()
{
$publicKey = \Sodium\crypto_box_publickey_from_secretkey(
$this->get()
);
return new EncryptionPublicKey($publicKey);
}
}
11 changes: 11 additions & 0 deletions src/Asymmetric/SecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use \ParagonIE\Halite\Contract;
use \ParagonIE\Halite\Key;
use \ParagonIE\Halite\Alerts\CannotPerformOperation;

class SecretKey extends Key implements Contract\KeyInterface
{
Expand All @@ -17,4 +18,14 @@ public function __construct($keyMaterial = '', ...$args)
: false;
parent::__construct($keyMaterial, false, $signing, true);
}

/**
* See the appropriate derived class.
*/
public function derivePublicKey()
{
throw new CannotPerformOperation(
'This is not implemented in the base class'
);
}
}
13 changes: 13 additions & 0 deletions src/Asymmetric/SignatureSecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ public function __construct($keyMaterial = '', ...$args)
{
parent::__construct($keyMaterial, true);
}

/**
* See the appropriate derived class.
*
* @return SignaturePublicKey
*/
public function derivePublicKey()
{
$publicKey = \Sodium\crypto_sign_publickey_from_secretkey(
$this->get()
);
return new SignaturePublicKey($publicKey);
}
}
26 changes: 24 additions & 2 deletions test/unit/KeyPairTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
use \ParagonIE\Halite\KeyFactory;
use \ParagonIE\Halite\KeyPair;
use \ParagonIE\Halite\Key;
use \ParagonIE\Halite\Asymmetric\Crypto as Asymmetric;
use \ParagonIE\Halite\Asymmetric\SecretKey as ASecretKey;
use \ParagonIE\Halite\Asymmetric\PublicKey as APublicKey;
Expand Down Expand Up @@ -56,4 +54,28 @@ public function testFileStorage()
);
\unlink($filename);
}

/**
* @covers \ParagonIE\Halite\Asymmetric\EncryptionSecretKey::derivePublicKey()
* @covers \ParagonIE\Halite\Asymmetric\SignatureSecretKey::derivePublicKey()
*/
public function testPublicDerivation()
{
$enc_kp = KeyFactory::generateEncryptionKeyPair();
$enc_secret = $enc_kp->getSecretKey();
$enc_public = $enc_kp->getPublicKey();

$this->assertEquals(
$enc_secret->derivePublicKey()->get(),
$enc_public->get()
);

$sign_kp = KeyFactory::generateSignatureKeyPair();
$sign_secret = $sign_kp->getSecretKey();
$sign_public = $sign_kp->getPublicKey();
$this->assertEquals(
$sign_secret->derivePublicKey()->get(),
$sign_public->get()
);
}
}

0 comments on commit 4f90faa

Please sign in to comment.