Skip to content

Instantly share code, notes, and snippets.

@ezimuel
Created September 27, 2013 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezimuel/6728959 to your computer and use it in GitHub Desktop.
Save ezimuel/6728959 to your computer and use it in GitHub Desktop.
Example to encrypt a file (even big) using Zend\Filter\Encrypt of ZF2 with a simple block schema (low memory consumption).
<?php
// include the ZF2 library
use Zend\Filter\Encrypt;
if (!isset($argv[1]) or !isset($argv[2])) {
die("Usage: " . basename(__FILE__) . " <file_to_encrypt> <encryption_key>\n");
}
if (!file_exists($argv[1])) {
die("The file {$argv[1]} specified doesn't exist\n");
}
$fileIn = $argv[1];
$fileOut = $fileIn . '.enc';
$buffer = 1048576; // 1 MB (increase if needed)
$filter = new Zend\Filter\Encrypt(array('adapter' => 'BlockCipher'));
$filter->setKey($argv[2]);
$read = @fopen($fileIn, "r");
$write = @fopen($fileOut, "w");
while (!feof($read)) {
$data = fread($read, $buffer);
fwrite($write, $filter->filter($data));
}
fclose($write);
fclose($read);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment