1. Go to this page and download the library: Download jsq/psr7-stream-encryption library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jsq / psr7-stream-encryption example snippets
$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));
$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()');
$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);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.