wubbajack / filecrypt
用于加密和解密文件的PHP包
0.2.0
2016-01-04 11:04 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.1
- scrutinizer/ocular: ^1.3
README
一个用于加密和解密文件的PHP包。同时提供文件数据的流式解密功能。
免责声明
此包仍在开发中。这意味着方法签名和返回值可能会随时间变化。具体实现也会变化。
如果您希望使用或测试此包,请谨慎操作。所有内容都已进行“人工”测试,仍在编写适当的测试。
如果您希望贡献,请在发送pull请求之前先创建新问题。
信息
此项目的目标是提供一个简单的方法来在PHP中处理加密文件。现在有一些优秀的(shell)工具可以实现相同的功能。但是,当需要解密流时,我发现实现起来越来越困难。
此包使用流过滤器进行加密和解密,使用box标准mcrypt加密。这允许流式解密。
默认加密
默认情况下,该包使用AES加密标准。这意味着使用此包加密的文件可以通过支持AES的任何其他工具解密,前提是你有密钥和IV的字符串表示。
安装和需求
您可以使用composer轻松安装此包
$ composer require wubbajack/filecrypt
最低要求如下
- PHP 5.6
- Mcrypt扩展
测试
要测试此包,只需运行
$ php vendor/bin/phpunit
示例和用法
以下是使用FileEncrypter类的示例
加密文件
<?php /** * This creates a new instance of the FileEncrypter. By default * it uses RIJNDAEL-128 with a 16 bit block size, which corresponds with the AES standard. */ $fileEncrypter = new Wubbajack\Encryption\FileEncrypter($key); $source_file = '/path/to/source/file.jpg'; $target_file = '/path/to/encryted/file.enc'; /** * Encrypts a source file to a target file and returns an EncryptedFile instance */ $encryptedFile = $fileEncrypter->encrypt($source_file, $target_file);
解密文件
<?php /** * In this example we assume that we already have an EncryptedFile instance * where we can extract the required information from */ $fileCrypt = new Wubbajack\Encryption\FileEncrypter($key); $target_file = '/path/to/decrypted/file.jpg'; // Decrypts our encrypted file and returns the path to the file $fileCrypt->decrypt($encryptedFile, $target_file);
流式解密
<?php /** * In this example we also assume that we already have an EncryptedFile instance */ $fileCrypt = new Wubbajack\Encryption\FileEncrypter($key); /** * The streamDecrypt method allows you to supply a callback and manipulate or echo the data. * This can be very useful when streaming encrypted media back to a client. * * The padding is automatically stripped from the data, so no worries there. */ $fileCrypt->streamDecrypt($encryptedFile, function ($data, $stream) { echo $data; if (feof($stream)) { // I have finished! } });