ParagonIE\Halite\File::unseal PHP Method

unseal() public static method

Decrypt a file using anonymous public-key encryption. Ciphertext integrity is still assured thanks to the Encrypt-then-MAC construction.
public static unseal ( string | resource $input, string | resource $output, EncryptionSecretKey $secretKey ) : boolean
$input string | resource File name or file handle
$output string | resource File name or file handle
$secretKey EncryptionSecretKey Recipient's encryption secret key
return boolean TRUE on success
    public static function unseal($input, $output, EncryptionSecretKey $secretKey) : bool
    {
        if ((\is_resource($input) || \is_string($input)) && (\is_resource($output) || \is_string($output))) {
            $readOnly = new ReadOnlyFile($input);
            $mutable = new MutableFile($output);
            try {
                $data = self::unsealData($readOnly, $mutable, $secretKey);
                return $data;
            } finally {
                $readOnly->close();
                $mutable->close();
            }
        }
        throw new InvalidType('Argument 1: Expected a filename or resource');
    }

Usage Example

Example #1
0
 public function testSealFail()
 {
     \touch(__DIR__ . '/tmp/paragon_avatar.seal_fail.png');
     \chmod(__DIR__ . '/tmp/paragon_avatar.seal_fail.png', 0777);
     \touch(__DIR__ . '/tmp/paragon_avatar.open_fail.png');
     \chmod(__DIR__ . '/tmp/paragon_avatar.open_fail.png', 0777);
     $keypair = KeyFactory::generateEncryptionKeyPair();
     $secretkey = $keypair->getSecretKey();
     $publickey = $keypair->getPublicKey();
     File::seal(__DIR__ . '/tmp/paragon_avatar.png', __DIR__ . '/tmp/paragon_avatar.seal_fail.png', $publickey);
     $fp = \fopen(__DIR__ . '/tmp/paragon_avatar.seal_fail.png', 'ab');
     \fwrite($fp, \Sodium\randombytes_buf(1));
     fclose($fp);
     try {
         File::unseal(__DIR__ . '/tmp/paragon_avatar.seal_fail.png', __DIR__ . '/tmp/paragon_avatar.opened.png', $secretkey);
         $this->fail('Possible authentication bypass in File::unseal()!');
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }
All Usage Examples Of ParagonIE\Halite\File::unseal