jsq/psr7-stream-encryption
最新稳定版本:0.4.0
Composer 安装命令:
composer require jsq/psr7-stream-encryption
包简介
For encrypting and decrypting streams of arbitrary size.
关键字:
README 文档
README
PHP's built-in OpenSSL bindings provide a convenient means of encrypting and decrypting data. The interface provided by ext-openssl, however, only operates on strings, so decrypting a large ciphertext would require loading the entire ciphertext into memory and receiving a string containing the entirety of the decoded plaintext.
This package aims to allow the encryption and decryption of streams of arbitrary size. It supports streaming encryption and decryption using AES-CBC, AES-CTR, and AES-ECB.
Using AES-ECB is NOT RECOMMENDED for new systems. It is included to allow interoperability with older systems. Please consult Wikipedia for a discussion of the drawbacks of ECB.
Usage
Decorate an instance of Psr\Http\Message\StreamInterface with an encrypting decorator to incrementally encrypt the contents of the decorated stream as read is called on the decorating stream:
$iv = random_bytes(openssl_cipher_iv_length('aes-256-cbc')); $cipherMethod = new Cbc($iv); $key = 'some-secret-password-here'; $inStream = new Stream(fopen('some-input-file', 'r')); // Any PSR-7 stream will be fine here $cipherTextStream = new AesEncryptingStream($inStream, $key, $cipherMethod); // Wrap the stream in an EncryptingStream $cipherTextFile = Psr7\stream_for(fopen('encrypted.file', 'w')); Psr7\copy_to_stream($cipherTextStream, $cipherTextFile); // When you read from the encrypting stream, the data will be encrypted. // You'll also need to store the IV somewhere, because we'll need it later to decrypt the data. // In this case, I'll base64 encode it and stick it in a file (but we could put it anywhere where we can retrieve it later, like a database column) file_put_contents('encrypted.iv', base64_encode($iv));
No encryption is performed until read is called on the encrypting stream.
To calculate the HMAC of a cipher text, wrap a decorated stream with an instance of HashingStream:
$hash = null; $ciphertext = new Jsq\EncryptionStreams\AesEncryptingStream( $plaintext, $key, $cipherMethod ); $hashingDecorator = new Jsq\EncryptionStreams\HashingStream( $ciphertext, $key, function ($calculatedHash) use (&$hash) { $hash = $calculatedHash; } ); while (!$ciphertext->eof()) { $ciphertext->read(1024 * 1024); } assert('$hash === $hashingDecorator->getHash()');
When decrypting a cipher text, wrap the cipher text in a hasing decorator before passing it as an argument to the decrypting stream:
$key = 'secret key'; $iv = random_bytes(openssl_cipher_iv_length('aes-256-cbc')); $plainText = 'Super secret text'; $cipherText = openssl_encrypt( $plainText, 'aes-256-cbc', $key, OPENSSL_RAW_DATA $iv ); $expectedHash = hash('sha256', $cipherText); $hashingDecorator = new Jsq\EncryptingStreams\HashingStream( GuzzleHttp\Psr7\stream_for($cipherText), $key, function ($hash) use ($expectedHash) { if ($hash !== $expectedHash) { throw new DomainException('Cipher text mac does not match expected value!'); } } ); $decrypted = new Jsq\EncryptionStreams\AesEncryptingStream( $cipherText, $key, $cipherMethod ); while (!$decrypted->eof()) { $decrypted->read(1024 * 1024); }
As with the encrypting decorators, HashingStreams are lazy and will only hash the underlying stream as it is read. In the example above, no exception would be thrown until the entire cipher text had been read (and all but the last block deciphered).
HashingStreams are not seekable, so you will need to wrap on in a GuzzleHttp\Psr7\CachingStream to support random access.
jsq/psr7-stream-encryption 适用场景与选型建议
jsq/psr7-stream-encryption 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131.25k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「stream」 「encryption」 「psr」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jsq/psr7-stream-encryption 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jsq/psr7-stream-encryption 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jsq/psr7-stream-encryption 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple PSR-16 cache implementations for WordPress transient the OOP way
Object-Oriented API for PHP streams
A simple PHP class to encrypt a string and decrypt an encrypted string
Safely namespaced guzzle psr7 for WP2Static
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Encryption with AES-256 and HMAC-SHA256
统计信息
- 总下载量: 131.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 17
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-01-04