unseal() public static method

Decrypt a sealed message with our private key
public static unseal ( string $ciphertext, EncryptionSecretKey $privateKey, mixed $encoding = Halite::ENCODE_BASE64URLSAFE ) : HiddenString
$ciphertext string Encrypted message
$privateKey EncryptionSecretKey
$encoding mixed Which encoding scheme to use?
return HiddenString
Example #1
0
 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);
     }
 }
Example #2
0
 public function testSealFail()
 {
     if (\Sodium\library_version_major() < 7 || \Sodium\library_version_major() == 7 && \Sodium\library_version_minor() < 5) {
         $this->markTestSkipped("Your version of libsodium is too old");
     }
     $alice = KeyFactory::generateEncryptionKeyPair();
     $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);
         $this->fail('This should have thrown an InvalidMessage exception!');
     } catch (CryptoException\InvalidKey $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidKey);
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }