PHPackages                             jsq/psr7-stream-encryption - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. jsq/psr7-stream-encryption

AbandonedArchivedLibrary[HTTP &amp; Networking](/categories/http)

jsq/psr7-stream-encryption
==========================

For encrypting and decrypting streams of arbitrary size.

0.4.0(6y ago)36138.2k↓11.9%14[1 issues](https://github.com/jeskew/php-encrypted-streams/issues)[5 PRs](https://github.com/jeskew/php-encrypted-streams/pulls)1Apache-2.0PHPPHP &gt;=7.1CI failing

Since Feb 12Pushed 1y ago2 watchersCompare

[ Source](https://github.com/jeskew/php-encrypted-streams)[ Packagist](https://packagist.org/packages/jsq/psr7-stream-encryption)[ RSS](/packages/jsq-psr7-stream-encryption/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (3)Versions (7)Used By (1)

PSR-7 Stream Encryption Decorators
==================================

[](#psr-7-stream-encryption-decorators)

[![Build Status](https://camo.githubusercontent.com/a076e327d57ebda62c10e7f0ef7605057355553cc1ff04864872a0b19a1680af/68747470733a2f2f7472617669732d63692e6f72672f6a65736b65772f7068702d656e637279707465642d73747265616d732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jeskew/php-encrypted-streams)[![Total Downloads](https://camo.githubusercontent.com/0e1880a17d36d122282e0a3cdd10b9f1e2b66c0677005dfb94bf768475a6d0b6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a73712f707372372d73747265616d2d656e6372797074696f6e2e7376673f7374796c653d666c6174)](https://packagist.org/packages/jsq/psr7-stream-encryption)[![Author](https://camo.githubusercontent.com/297a2017c3c5d5b0f717bf17f2713cd12b5ba24945e861cf9af88e37d6bf50ce/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406a7265736b65772d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/jreskew)

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](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29)for a discussion of the drawbacks of ECB.

Usage
-----

[](#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, `HashingStream`s 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).

`HashingStream`s are not seekable, so you will need to wrap on in a `GuzzleHttp\Psr7\CachingStream` to support random access.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~278 days

Total

5

Last Release

2316d ago

PHP version history (2 changes)0.1.0PHP &gt;=5.5

0.3.1PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/76a37eaa8fb7a04df094d25e1b53a908621faa9961da919f50c1c6c762ffbdfe?d=identicon)[jeskew](/maintainers/jeskew)

---

Top Contributors

[![jeskew](https://avatars.githubusercontent.com/u/705500?v=4)](https://github.com/jeskew "jeskew (34 commits)")

---

Tags

psrpsr-7streamencryption

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jsq-psr7-stream-encryption/health.svg)

```
[![Health](https://phpackages.com/badges/jsq-psr7-stream-encryption/health.svg)](https://phpackages.com/packages/jsq-psr7-stream-encryption)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.1B4.0k](/packages/guzzlehttp-psr7)[algolia/algoliasearch-client-php

API powering the features of Algolia.

69735.1M159](/packages/algolia-algoliasearch-client-php)[gotenberg/gotenberg-php

A PHP client for interacting with Gotenberg, a developer-friendly API for converting numerous document formats into PDF files, and more!

3856.2M31](/packages/gotenberg-gotenberg-php)[mezzio/mezzio

PSR-15 Middleware Microframework

3923.8M126](/packages/mezzio-mezzio)[oat-sa/tao-core

TAO core extension

66143.7k124](/packages/oat-sa-tao-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
