-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to the key being an interface
- Loading branch information
1 parent
0fc4a77
commit 434131d
Showing
13 changed files
with
515 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* @project Legatus Crypto | ||
* @link https://github.com/legatus-php/crypto | ||
* @package legatus/crypto | ||
* @author Matias Navarro-Carter mnavarrocarter@gmail.com | ||
* @license MIT | ||
* @copyright 2021 Matias Navarro-Carter | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Legatus\Support\Base64; | ||
|
||
use InvalidArgumentException; | ||
use RuntimeException; | ||
use SodiumException; | ||
|
||
/** | ||
* @param string $message | ||
* | ||
* @return string | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
function url_encode(string $message): string | ||
{ | ||
try { | ||
return sodium_bin2base64($message, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING); | ||
} catch (SodiumException $e) { | ||
throw new RuntimeException('Could not encode message'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $encoded | ||
* | ||
* @return string | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
function url_decode(string $encoded): string | ||
{ | ||
try { | ||
return sodium_base642bin($encoded, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING); | ||
} catch (SodiumException $e) { | ||
throw new InvalidArgumentException('Invalid base64 encoded string'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* @project Legatus Crypto | ||
* @link https://github.com/legatus-php/crypto | ||
* @package legatus/crypto | ||
* @author Matias Navarro-Carter mnavarrocarter@gmail.com | ||
* @license MIT | ||
* @copyright 2021 Matias Navarro-Carter | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Legatus\Support; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Class RotatedKeys. | ||
*/ | ||
final class RotatedKeys implements SecretKey | ||
{ | ||
/** | ||
* @var SecretKey[] | ||
*/ | ||
private array $keys; | ||
|
||
/** | ||
* RotatedKeys constructor. | ||
* | ||
* @param SecretKey ...$keys | ||
*/ | ||
public function __construct(SecretKey ...$keys) | ||
{ | ||
$this->keys = $keys; | ||
} | ||
|
||
/** | ||
* @param SecretKey $key | ||
*/ | ||
public function push(SecretKey $key): void | ||
{ | ||
$this->keys[] = $key; | ||
} | ||
|
||
/** | ||
* @return SecretKey | ||
*/ | ||
public function getCurrentKey(): SecretKey | ||
{ | ||
$count = count($this->keys); | ||
if ($count === 0) { | ||
throw new \RuntimeException('There are no keys present'); | ||
} | ||
|
||
return $this->keys[$count - 1]; | ||
} | ||
|
||
/** | ||
* @return SecretKey[] | ||
*/ | ||
public function getAllKeys(): array | ||
{ | ||
return array_reverse($this->keys); | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param string $nonce | ||
* | ||
* @return string | ||
*/ | ||
public function encrypt(string $message, string $nonce): string | ||
{ | ||
return $this->getCurrentKey()->encrypt($message, $nonce); | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* | ||
* @return string | ||
*/ | ||
public function authenticate(string $message): string | ||
{ | ||
return $this->getCurrentKey()->authenticate($message); | ||
} | ||
|
||
/** | ||
* @param string $authenticatedMessage | ||
* | ||
* @return string | ||
*/ | ||
public function verify(string $authenticatedMessage): string | ||
{ | ||
foreach ($this->getAllKeys() as $key) { | ||
try { | ||
return $key->verify($authenticatedMessage); | ||
} catch (InvalidArgumentException $e) { | ||
continue; | ||
} | ||
} | ||
throw new InvalidArgumentException('Could not verify message'); | ||
} | ||
|
||
/** | ||
* @param string $cipher | ||
* @param string $nonce | ||
* | ||
* @return string | ||
*/ | ||
public function decrypt(string $cipher, string $nonce): string | ||
{ | ||
foreach ($this->getAllKeys() as $key) { | ||
try { | ||
return $key->decrypt($cipher, $nonce); | ||
} catch (InvalidArgumentException $e) { | ||
continue; | ||
} | ||
} | ||
throw new InvalidArgumentException('Could not decrypt message'); | ||
} | ||
} |
Oops, something went wrong.