Created
September 27, 2013 13:54
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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