ParagonIE\Halite\File::encrypt PHP Method

encrypt() public static method

Encrypt a file using symmetric authenticated encryption.
public static encrypt ( string | resource $input, string | resource $output, EncryptionKey $key ) : integer
$input string | resource File name or file handle
$output string | resource File name or file handle
$key EncryptionKey Symmetric encryption key
return integer Number of bytes written
    public static function encrypt($input, $output, EncryptionKey $key) : int
    {
        if ((\is_resource($input) || \is_string($input)) && (\is_resource($output) || \is_string($output))) {
            $readOnly = new ReadOnlyFile($input);
            $mutable = new MutableFile($output);
            $data = self::encryptData($readOnly, $mutable, $key);
            $readOnly->close();
            $mutable->close();
            return $data;
        }
        throw new InvalidType('Argument 1: Expected a filename or resource');
    }

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::encrypt