jsq / psr6-encrypting-decorator
当策略或疑虑阻止您将敏感的缓存条目以未加密的形式存储时使用。
Requires
- php: ^5.5|^7.0
- ext-openssl: *
- psr/cache: ^1.0
Requires (Dev)
- cache/doctrine-adapter: ^0.3.0
- cache/integration-tests: ^0.2.0
- doctrine/cache: ^1.6
- paragonie/random_compat: ^2.0
- phpunit/phpunit: ^4.8
Suggests
- paragonie/random_compat: To use with PHP <7.0
This package is auto-updated.
Last update: 2024-08-29 04:14:29 UTC
README
您需要加密静态数据,但这不应该阻止您使用您熟悉和喜爱的开源工具。如果您有需要比其他缓存更高安全级别的数据,您可以通过加密的 PoolDecorator
来存储和访问它。
注意事项
加密和解密都是昂贵的操作,并且频繁地从加密的数据存储中读取可能会快速成为性能良好的应用程序的瓶颈。请谨慎使用加密缓存。
用法
此包提供两个缓存装饰器,一个使用密码加密数据,另一个使用密钥对加密。
首先,像平时一样创建您的PSR-6缓存,然后使用加密装饰器包装您的缓存
$encryptedCache = new \Jsq\CacheEncryption\Password\PoolDecorator( $cache, // an instance of \Psr\Cache\CacheItemPoolInterface $password, $cipher // optional, defaults to 'aes-256-cbc' );
然后像平时一样使用您的 $cache
和 $encryptedCache
$cache->save($cache->getItem('normal_cache_data')->set('Totally normal!')); $encryptedCache->save($encryptedCache->getItem('api_key')->set('super_secret'));
尽管您的常规缓存和加密缓存共享存储层和密钥空间,但它们无法读取彼此的数据。如果底层数据未加密,$encryptedCache
将对 isHit
返回 false
,如果请求读取加密数据,常规的 $cache
将返回乱码。
var_dump($encryptedCache->getItem('api_key')->get()); // string(12) "super_secret" var_dump($cache->getItem('api_key')->get()); // class Jsq\CacheEncryption\Password\EncryptedValue#177 (4) { // private $mac => // string(64) <hexits> // private $cipherText => // string(44) <base64 encoded value> // private $method => // string(11) "aes-256-cbc" // private $initializationVector => // string(16) <binary string> // } var_dump($cache->getItem('normal_cache_data')->isHit()); // bool(true) var_dump($encryptedCache->getItem('normal_cache_data')->isHit()); // bool(false)
使用密钥对加密缓存
如果您不希望依赖共享密码,则 Envelope\PoolDecorator
可以使用公钥/私钥对来保护您的敏感缓存条目。
$encryptedCache = new \Jsq\CacheEncryption\Envelope\PoolDecorator( $cache, 'file:///path/to/certificate.pem', 'file:///path/to/private/key.pem', $passphrase_for_private_key_file, // optional, defaults to null $cipher // optional, defaults to 'aes-256-cbc' );
证书可以是有效的x509证书,PEM编码证书文件的路径(路径必须以
file://
开头),或者PEM编码的证书字符串。私钥可以是PEM编码私钥文件的路径(路径必须以file://
开头),或PEM编码的证书字符串。
将缓存作为铁令牌加密
此库还支持与Iron库兼容的加密。如果您希望在多个应用程序之间共享缓存,即使这些应用程序是用不同语言编写的,这可能会很有用。Iron规范详细说明了如何密封JSON对象,因此Iron装饰器将在加密之前将传递给它的数据编码为JSON。解密的项目将以数组的形式返回。