diff --git a/src/SymmetricKeyEncryption.php b/src/SymmetricKeyEncryption.php index 6426ebf..cd951e7 100644 --- a/src/SymmetricKeyEncryption.php +++ b/src/SymmetricKeyEncryption.php @@ -13,6 +13,7 @@ use ParagonIE\Halite\Symmetric\Crypto; use ParagonIE\Halite\Symmetric\EncryptionKey; use ParagonIE\HiddenString\HiddenString; +use SensitiveParameter; use SodiumException; use Spaze\Encryption\Exceptions\InvalidNumberOfComponentsException; use Spaze\Encryption\Exceptions\UnknownEncryptionKeyIdException; @@ -25,6 +26,9 @@ class SymmetricKeyEncryption private const KEY_CIPHERTEXT_SEPARATOR = '$'; + /** @var array> */ + private array $keys = []; + /** * @param array> $keys key group => key id => key @@ -32,9 +36,14 @@ class SymmetricKeyEncryption */ public function __construct( private string $keyGroup, - private array $keys, + #[SensitiveParameter] array $keys, private array $activeKeyIds, ) { + foreach ($keys as $name => $group) { + foreach ($group as $id => $key) { + $this->keys[$name][$id] = new HiddenString(Hex::decode($key)); + } + } } @@ -48,7 +57,7 @@ public function __construct( * @throws TypeError * @throws UnknownEncryptionKeyIdException */ - public function encrypt(string $data): string + public function encrypt(#[SensitiveParameter] string $data): string { $keyId = $this->getActiveKeyId(); $key = $this->getKey($keyId); @@ -97,7 +106,7 @@ public function needsReEncrypt(string $data): bool private function getKey(string $keyId): EncryptionKey { if (isset($this->keys[$this->keyGroup][$keyId])) { - return new EncryptionKey(new HiddenString(Hex::decode($this->keys[$this->keyGroup][$keyId]))); + return new EncryptionKey($this->keys[$this->keyGroup][$keyId]); } else { throw new UnknownEncryptionKeyIdException($keyId); } diff --git a/tests/SymmetricKeyEncryptionTest.phpt b/tests/SymmetricKeyEncryptionTest.phpt index 92ba737..89f4077 100644 --- a/tests/SymmetricKeyEncryptionTest.phpt +++ b/tests/SymmetricKeyEncryptionTest.phpt @@ -34,7 +34,7 @@ class SymmetricKeyEncryptionTest extends TestCase protected function setUp(): void { $this->keys = [ - 'token' => [ + self::KEY_GROUP => [ self::INACTIVE_KEY => bin2hex(random_bytes(32)), self::ACTIVE_KEY => bin2hex(random_bytes(32)), ], @@ -101,6 +101,27 @@ class SymmetricKeyEncryptionTest extends TestCase ]; } + + public function testEncryptSensitiveParameter(): void + { + $e = Assert::exception( + function () { + (new SymmetricKeyEncryption(self::KEY_GROUP, $this->keys, [self::KEY_GROUP => 'foo']))->encrypt(self::PLAINTEXT); + }, + UnknownEncryptionKeyIdException::class, + ); + Assert::notContains(self::PLAINTEXT, $e->getTraceAsString()); + Assert::contains('SensitiveParameterValue', $e->getTraceAsString()); + } + + + public function testHiddenStringKeys(): void + { + $object = print_r(new SymmetricKeyEncryption(self::KEY_GROUP, $this->keys, [self::KEY_GROUP => self::ACTIVE_KEY]), true); + Assert::notContains($this->keys[self::KEY_GROUP][self::ACTIVE_KEY], $object); + Assert::notContains($this->keys[self::KEY_GROUP][self::INACTIVE_KEY], $object); + } + } (new SymmetricKeyEncryptionTest())->run();