ParagonIE\Halite\File::seal PHP Method

seal() public static method

Encrypt a file using anonymous public-key encryption (with ciphertext authentication).
public static seal ( string | resource $input, string | resource $output, EncryptionPublicKey $publicKey ) : integer
$input string | resource File name or file handle
$output string | resource File name or file handle
$publicKey EncryptionPublicKey Recipient's encryption public key
return integer Number of bytes written
    public static function seal($input, $output, EncryptionPublicKey $publicKey) : int
    {
        if ((\is_resource($input) || \is_string($input)) && (\is_resource($output) || \is_string($output))) {
            $readOnly = new ReadOnlyFile($input);
            $mutable = new MutableFile($output);
            $data = self::sealData($readOnly, $mutable, $publicKey);
            $readOnly->close();
            $mutable->close();
            return $data;
        }
        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);
     }
 }