Skip to content
This repository was archived by the owner on Mar 20, 2026. It is now read-only.

Commit 432b7b9

Browse files
committed
Update first usage example
1 parent 2f83ed0 commit 432b7b9

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,22 @@ for a discussion of the drawbacks of ECB.
2121
## Usage
2222

2323
Decorate an instance of `Psr\Http\Message\StreamInterface` with an encrypting
24-
decorator to incrementally encrypt the contents of the decorated stream as
24+
decorator to incrementally encrypt the contents of the decorated stream as
2525
`read` is called on the decorating stream:
2626

2727
```php
28-
$cipherMethod = new Jsq\EncryptionStreams\Cbc(
29-
random_bytes(openssl_cipher_iv_length('aes-256-cbc'))
30-
);
31-
$key = ... // a symmetric encryption key
32-
// Create a PSR-7 stream for a very large file.
33-
$plaintext = new GuzzleHttp\Psr7\LazyOpenStream('/path/to/a/massive/file', 'r+);
34-
// Create an encrypting stream.
35-
$ciphertext = new Jsq\EncryptionStreams\AesEncryptingStream(
36-
$plaintext,
37-
$key,
38-
$cipherMethod
39-
);
28+
$iv = random_bytes(openssl_cipher_iv_length('aes-256-cbc'));
29+
$cipherMethod = new Cbc($iv);
30+
$key = 'some-secret-password-here';
31+
32+
$inStream = new Stream(fopen('some-input-file', 'r')); // Any PSR-7 stream will be fine here
33+
$cipherTextStream = new AesEncryptingStream($inStream, $key, $cipherMethod); // Wrap the stream in an EncryptingStream
34+
$cipherTextFile = Psr7\stream_for(fopen('encrypted.file', 'w'));
35+
Psr7\copy_to_stream($cipherTextStream, $cipherTextFile); // When you read from the encrypting stream, the data will be encrypted.
4036

41-
$encryptedChunk = $ciphertext->read(1024 * 1024);
37+
// You'll also need to store the IV somewhere, because we'll need it later to decrypt the data.
38+
// 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)
39+
file_put_contents('encrypted.iv', base64_encode($iv));
4240
```
4341

4442
No encryption is performed until `read` is called on the encrypting stream.

0 commit comments

Comments
 (0)