ParagonIE\Halite\File::decrypt PHP Method

decrypt() public static method

Decrypt a file using symmetric-key authenticated encryption.
public static decrypt ( string | resource $input, string | resource $output, EncryptionKey $key ) : boolean
$input string | resource File name or file handle
$output string | resource File name or file handle
$key EncryptionKey Symmetric encryption key
return boolean TRUE if successful
    public static function decrypt($input, $output, EncryptionKey $key) : bool
    {
        if ((\is_resource($input) || \is_string($input)) && (\is_resource($output) || \is_string($output))) {
            try {
                $readOnly = new ReadOnlyFile($input);
                $mutable = new MutableFile($output);
                $data = self::decryptData($readOnly, $mutable, $key);
                return $data;
            } finally {
                $readOnly->close();
                $mutable->close();
            }
        }
        throw new InvalidType('Strings or file handles expected');
    }

Usage Example

Example #1
0
 public function testEncryptFail()
 {
     \touch(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png');
     \chmod(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', 0777);
     \touch(__DIR__ . '/tmp/paragon_avatar.decrypt_fail.png');
     \chmod(__DIR__ . '/tmp/paragon_avatar.decrypt_fail.png', 0777);
     $key = new EncryptionKey(\str_repeat('B', 32));
     File::encrypt(__DIR__ . '/tmp/paragon_avatar.png', __DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', $key);
     $fp = \fopen(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', 'ab');
     \fwrite($fp, \Sodium\randombytes_buf(1));
     fclose($fp);
     try {
         File::decrypt(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', __DIR__ . '/tmp/paragon_avatar.decrypt_fail.png', $key);
         $this->fail('Possible authentication bypass in File::decrypt()!');
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }
All Usage Examples Of ParagonIE\Halite\File::decrypt