简体   繁体   中英

Encrypting native PHP streams

I'm investigating the possibility of symmetrically encrypting native streams in PHP. I've found lots of information showing how to perform this sort of encryption on normal strings (entirely in memory) using modern and recommended libraries such as sodium and openssl, but the only mention I've discovered about encrypting native PHP streams is here but that page and the comments suggest that not using it is safer (and it's marked as deprecated).

I haven't been able to find practical examples of how to use native PHP encryption filters on streams doing it safely.

I've found this library which works on the Guzzle PSR-7 stream object but not on native PHP streams. Unfortunately this isn't interoperable with other libraries which expect native streams.

I'm interested in streams as opposed to encrypting full files because that avoids all memory and size limits.

Is encryption on streams possible using stream encryption filters as mentioned in the docs but just not recommended and deprecated because it's so easy to get wrong or is it simply not possible to do securely?

After further investigation it turns out that it is possible to transform a StreamInterface into a native PHP stream by using the StreamWrapper here: https://github.com/guzzle/psr7/blob/master/src/StreamWrapper.php

This solves my problem as any library which uses the PSR StreamInterface can be used, I just need to convert back to a native stream before passing it to any other library which expects native streams.

You may want to look into stream filters, you can setup essentially a piping arrangement... bonus, is with SteamWrappers you can apply your filters to any stream which php uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM