public static seal ( HiddenString $plaintext, |
||
$plaintext | HiddenString | Message to encrypt |
$publicKey | Public encryption key | |
$encoding | mixed | Which encoding scheme to use? |
return | string | Ciphertext |
public static function seal(HiddenString $plaintext, EncryptionPublicKey $publicKey, $encoding = Halite::ENCODE_BASE64URLSAFE) : string
{
if (!$publicKey instanceof EncryptionPublicKey) {
throw new InvalidKey('Argument 2: Expected an instance of EncryptionPublicKey');
}
$sealed = \Sodium\crypto_box_seal($plaintext->getString(), $publicKey->getRawKeyMaterial());
$encoder = Halite::chooseEncoder($encoding);
if ($encoder) {
return $encoder($sealed);
}
return $sealed;
}
public function testSealFail() { $alice = KeyPair::generate(); $message = 'This is for your eyes only'; $sealed = Asymmetric::seal($message, $alice->getPublicKey(), true); // Let's flip one bit, randomly: $r = \Sodium\randombytes_uniform(\mb_strlen($sealed, '8bit')); $amt = 1 << \Sodium\randombytes_uniform(8); $sealed[$r] = \chr(\ord($sealed[$r]) ^ $amt); // This should throw an exception try { $opened = Asymmetric::unseal($sealed, $alice->getSecretKey(), true); $this->assertEquals($opened, $message); throw new Exception('ERROR: THIS SHOULD ALWAYS FAIL'); } catch (CryptoException\InvalidKey $e) { $this->assertTrue($e instanceof CryptoException\InvalidKey); } catch (CryptoException\InvalidMessage $e) { $this->assertTrue($e instanceof CryptoException\InvalidMessage); } }