Skip to content

Commit

Permalink
Merge pull request #154 from paragonie/version-helper
Browse files Browse the repository at this point in the history
Add isForVersion() helper method to keys
  • Loading branch information
paragonie-security authored Apr 26, 2022
2 parents f73a309 + 97ba6e9 commit cd48535
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 11 deletions.
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<psalm
name="Paseto Static Analysis"
useDocblockTypes="true"
totallyTyped="true"
>
<projectFiles>
<directory name="src" />
Expand Down
9 changes: 9 additions & 0 deletions src/Keys/AsymmetricPublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ public function getProtocol(): ProtocolInterface
return $this->protocol;
}

/**
* @param ProtocolInterface $protocol
* @return bool
*/
public function isForVersion(ProtocolInterface $protocol): bool
{
return $this->protocol instanceof $protocol;
}

/**
* Get the raw key contents.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Keys/AsymmetricSecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ public function getProtocol(): ProtocolInterface
return $this->protocol;
}

/**
* @param ProtocolInterface $protocol
* @return bool
*/
public function isForVersion(ProtocolInterface $protocol): bool
{
return $this->protocol instanceof $protocol;
}

/**
* Get the public key that corresponds to this secret key.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Keys/SymmetricKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ public function getProtocol(): ProtocolInterface
return $this->protocol;
}

/**
* @param ProtocolInterface $protocol
* @return bool
*/
public function isForVersion(ProtocolInterface $protocol): bool
{
return $this->protocol instanceof $protocol;
}

/**
* Get the raw key contents.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Protocol/Version1.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public function __construct()
/**
* Get the number of bytes in a symmetric key.
*
* @return int
* @return positive-int
*/
public static function getSymmetricKeyByteLength(): int
{
return (int) static::SYMMETRIC_KEY_BYTES;
return static::SYMMETRIC_KEY_BYTES;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Protocol/Version2.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public static function supportsImplicitAssertions(): bool
}

/**
* @return int
* @return positive-int
*/
public static function getSymmetricKeyByteLength(): int
{
return (int) static::SYMMETRIC_KEY_BYTES;
return static::SYMMETRIC_KEY_BYTES;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Protocol/Version3.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public function __construct()
}

/**
* @return int
* @return positive-int
*/
public static function getSymmetricKeyByteLength(): int
{
return (int) static::SYMMETRIC_KEY_BYTES;
return static::SYMMETRIC_KEY_BYTES;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Protocol/Version4.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public function __construct()
}

/**
* @return int
* @return positive-int
*/
public static function getSymmetricKeyByteLength(): int
{
return (int) static::SYMMETRIC_KEY_BYTES;
return static::SYMMETRIC_KEY_BYTES;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ProtocolInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function generateAsymmetricSecretKey(): AsymmetricSecretKey;
public static function generateSymmetricKey(): SymmetricKey;

/**
* @return int
* @return positive-int
*/
public static function getSymmetricKeyByteLength(): int;

Expand Down
15 changes: 15 additions & 0 deletions tests/Version1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use ParagonIE\Paseto\Keys\Version1\SymmetricKey;
use ParagonIE\Paseto\Protocol\Version1;
use ParagonIE\Paseto\Protocol\Version2;
use ParagonIE\Paseto\Protocol\Version3;
use ParagonIE\Paseto\Protocol\Version4;
use PHPUnit\Framework\TestCase;

class Version1Test extends TestCase
Expand All @@ -30,6 +32,19 @@ public function testKeyGen()

$this->assertSame(Version1::getSymmetricKeyByteLength(), Binary::safeStrlen($symmetric->raw()));
$this->assertGreaterThanOrEqual(1670, Binary::safeStrlen($secret->raw())); // PEM encoded

$mapping = [
[new Version1, true],
[new Version2, false],
[new Version3, false],
[new Version4, false],
];
foreach ($mapping as $row) {
[$version, $expected] = $row;
$this->assertSame($expected, $symmetric->isForVersion($version));
$this->assertSame($expected, $secret->isForVersion($version));
$this->assertSame($expected, $secret->getPublicKey()->isForVersion($version));
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Version2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use ParagonIE\Paseto\Keys\Version2\SymmetricKey;
use ParagonIE\Paseto\Protocol\Version1;
use ParagonIE\Paseto\Protocol\Version2;
use ParagonIE\Paseto\Protocol\Version3;
use ParagonIE\Paseto\Protocol\Version4;
use PHPUnit\Framework\TestCase;

class Version2Test extends TestCase
Expand All @@ -30,6 +32,19 @@ public function testKeyGen()

$this->assertSame(Version2::getSymmetricKeyByteLength(), Binary::safeStrlen($symmetric->raw()));
$this->assertSame(64, Binary::safeStrlen($secret->raw()));

$mapping = [
[new Version1, false],
[new Version2, true],
[new Version3, false],
[new Version4, false],
];
foreach ($mapping as $row) {
[$version, $expected] = $row;
$this->assertSame($expected, $symmetric->isForVersion($version));
$this->assertSame($expected, $secret->isForVersion($version));
$this->assertSame($expected, $secret->getPublicKey()->isForVersion($version));
}
}

public function testPublicKeyEncode()
Expand Down
17 changes: 16 additions & 1 deletion tests/Version3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use ParagonIE\Paseto\Keys\Version3\AsymmetricSecretKey;
use ParagonIE\Paseto\Keys\Version3\SymmetricKey;
use ParagonIE\Paseto\Protocol\{
Version1,
Version2,
Version3
Version3,
Version4
};
use PHPUnit\Framework\TestCase;

Expand All @@ -35,6 +37,19 @@ public function testKeyGen()
$asymmetric2 = new AsymmetricSecretKey("\x7f" . random_bytes(47), new Version3);
$pk = $asymmetric2->getPublicKey();
$this->assertInstanceOf(BasePK::class, $pk);

$mapping = [
[new Version1, false],
[new Version2, false],
[new Version3, true],
[new Version4, false],
];
foreach ($mapping as $row) {
[$version, $expected] = $row;
$this->assertSame($expected, $symmetric->isForVersion($version));
$this->assertSame($expected, $secret->isForVersion($version));
$this->assertSame($expected, $secret->getPublicKey()->isForVersion($version));
}
}

public function testPublicKeyEncode()
Expand Down
15 changes: 15 additions & 0 deletions tests/Version4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use ParagonIE\Paseto\Keys\Version4\AsymmetricSecretKey;
use ParagonIE\Paseto\Keys\Version4\SymmetricKey;
use ParagonIE\Paseto\Protocol\{
Version1,
Version2,
Version3,
Version4
};
Expand All @@ -31,6 +33,19 @@ public function testKeyGen()
$this->assertInstanceOf('ParagonIE\Paseto\Keys\AsymmetricSecretKey', $secret);
$this->assertSame(Version4::getSymmetricKeyByteLength(), Binary::safeStrlen($symmetric->raw()));
$this->assertGreaterThanOrEqual(48, Binary::safeStrlen($secret->raw())); // PEM encoded

$mapping = [
[new Version1, false],
[new Version2, false],
[new Version3, false],
[new Version4, true],
];
foreach ($mapping as $row) {
[$version, $expected] = $row;
$this->assertSame($expected, $symmetric->isForVersion($version));
$this->assertSame($expected, $secret->isForVersion($version));
$this->assertSame($expected, $secret->getPublicKey()->isForVersion($version));
}
}

/**
Expand Down

0 comments on commit cd48535

Please sign in to comment.