ilicmiljan/secure-props 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ilicmiljan/secure-props

Composer 安装命令:

composer require ilicmiljan/secure-props

包简介

Powerful PHP library designed to simplify the encryption and decryption of property data in objects.

README 文档

README

stability-mature GitHub Workflow Status (with branch) codecov Packagist PHP Version GitHub

SecureProps is a powerful PHP library designed to simplify the encryption and decryption of property data in objects.

Utilizing the power of PHP attributes, SecureProps allows developers to easily secure sensitive data within their applications. The library supports both asymmetric and symmetric encryption methods, providing flexibility in securing your application data.

Features

  • Easy-to-use encryption and decryption of properties within PHP objects.
  • Support for asymmetric encryption using RSA keys.
  • Support for symmetric encryption using Advanced Encryption Standard (AES-256-GCM).

Requirements

  • PHP 8.0 or higher.
  • OpenSSL extension enabled in your PHP installation.

Installation

You can install SecureProps via Composer by running the following command:

composer require ilicmiljan/secure-props

Ensure that your composer.json file is updated and the library is included in your project's dependencies.

Usage

Marking Properties for Encryption

Use the #[Encrypted] attribute to mark properties for encryption. This attribute supports an optional placeholder parameter for customizable decryption failure handling.

When decryption fails, and a placeholder is provided, that value is used instead. If the placeholder is null, an exception may be thrown.

use IlicMiljan\SecureProps\Attribute\Encrypted;

class User
{
    #[Encrypted(placeholder: "***-**-****")]
    private string $socialSecurityNumber;

    #[Encrypted]
    private string $secretNote;
}

Encrypting and Decrypting Objects

To encrypt or decrypt objects, you will need to use the ObjectEncryptionService. Here is an example:

use IlicMiljan\SecureProps\ObjectEncryptionService;
use IlicMiljan\SecureProps\Cipher\AdvancedEncryptionStandardCipher;

// Create a cipher instance (AES in this example)
$cipher = new AdvancedEncryptionStandardCipher('256-BIT-KEY-HERE');

// Initialize the encryption service with a runtime object properties reader
$encryptionService = new ObjectEncryptionService($cipher, new RuntimeObjectPropertiesReader());

$user = new User();
$user->setSocialSecurityNumber('123-45-6789');

// Encrypt properties
$encryptedUser = $encryptionService->encrypt($user);

// Decrypt properties
$decryptedUser = $encryptionService->decrypt($encryptedUser);

Asymmetric Encryption

To use asymmetric encryption, initialize the AsymmetricEncryptionCipher with your public and private keys:

use IlicMiljan\SecureProps\Cipher\AsymmetricEncryptionCipher;

$cipher = new AsymmetricEncryptionCipher($publicKey, $privateKey);

// Then, pass this cipher to the ObjectEncryptionService as shown above.

Tagged Encryption

When working with objects that have properties with both encrypted and non-encrypted values, the TagAwareCipher provides an advanced solution. This cipher allows for targeted decryption, operating only on encrypted and tagged properties, which prevents errors when encountering non-encrypted data.

How It Works

TagAwareCipher automatically tags encrypted data with <ENC> and </ENC>, making it distinctly identifiable. It specifically looks for these tags during the decryption process to determine which data to decrypt. This targeted approach means that if an object contains tagged (encrypted) and untagged (plain) data, TagAwareCipher will only attempt to decrypt the tagged portions.

This avoids the risk of exceptions that typically occur when trying to decrypt data that isn't encrypted, ensuring error-free processing.

Important

When using TagAwareCipher for decryption, it's crucial to understand that it will only decrypt data wrapped with <ENC> and </ENC> tags.

If it encounters encrypted data without these tags, the decryption process will skip it, and the data will be returned in its encrypted form.

Here is an example:

// Initialize the base cipher with AES encryption.
// Optionally, attach a NullEncoder to prevent data double-encoding. Useful when
// the decorator cipher (e.g., TagAwareCipher) applies its own encoding.
$baseCipher = new AdvancedEncryptionStandardCipher('256-BIT-KEY-HERE', new NullEncoder());

// Initialize TagAwareCipher with your base cipher and an optional custom encoder.
$cipher = new TagAwareCipher($baseCipher, new Base64Encoder());

// Initialize the encryption service with a runtime object properties reader.
$encryptionService = new ObjectEncryptionService($cipher, new RuntimeObjectPropertiesReader());

$user = new User();
$user->setSocialSecurityNumber('123-45-6789');

// Encrypt properties
$encryptedUser = $encryptionService->encrypt($user);

// Decrypt properties
$decryptedUser = $encryptionService->decrypt($encryptedUser);

Property Readers

SecureProps provides two types of property readers to handle encrypted properties within your PHP objects efficiently: RuntimeObjectPropertiesReader and CachingObjectPropertiesReader.

RuntimeObjectPropertiesReader

The RuntimeObjectPropertiesReader dynamically examines objects at runtime to identify properties decorated with the #[Encrypted] attribute. Utilizing PHP's reflection requires no additional setup for caching and offers straightforward inspection capabilities.

CachingObjectPropertiesReader

For enhanced performance, especially in applications that frequently deal with the same types of objects, the CachingObjectPropertiesReader caches property reading results. This approach reduces the computational overhead associated with reflection.

It integrates seamlessly with PSR-6 compliant caching solutions, allowing for customizable performance optimization.

Quick Start Example

Combining CachingObjectPropertiesReader with RuntimeObjectPropertiesReader and a PSR-6 compliant cache implementation:

// Initialize a PSR-6 cache pool
$cache = new FilesystemAdapter(...);

// Configure the caching reader
$reader = new CachingObjectPropertiesReader(
    new RuntimeObjectPropertiesReader(),
    new CacheItemPoolAdapter($cache)
);

// Set up the ObjectEncryptionService with the reader
$encryptionService = new ObjectEncryptionService($cipher, $reader);

Encoders

Encoders are crucial components in the encryption and decryption process, transforming data into a format suitable for secure transmission or storage and then back to its original form.

Base64Encoder

The Base64Encoder is designed for encoding binary data into a string of ASCII characters, using the Base64 encoding scheme. This makes the data safe for transmission over protocols that are not binary-safe.

NullEncoder

The NullEncoder serves as a pass-through, meaning it does not alter the input data. This is particularly useful when you want to avoid double-encoding data that is already in a suitable format for storage or when the encoding process is managed elsewhere.

Quick Start Example

In the context of initializing the AdvancedEncryptionStandardCipher with AES encryption, you can optionally attach a custom encoder.

// Preventing double-encoding by using NullEncoder with the base cipher
$cipher = new AdvancedEncryptionStandardCipher('256-BIT-KEY-HERE', new NullEncoder());


// Initializing the encryption service with the configured cipher and property reader
$encryptionService = new ObjectEncryptionService($cipher, new RuntimeObjectPropertiesReader());

// Example of encrypting and decrypting user data
$user = new User();
$user->setSocialSecurityNumber('123-45-6789');

// Encrypt properties. The operation returns a string that is not binary-safe,
// and may require encoding to be safely transmitted or stored.
$encryptedUser = $encryptionService->encrypt($user);

// Decrypt properties
$decryptedUser = $encryptionService->decrypt($encryptedUser);

Note

Unless specified otherwise, all ciphers use Base64Encoder as the default encoder to ensure the encrypted data is binary-safe and suitable for transmission or storage across different systems.

Contributing

Contributions to SecureProps are welcome. Please ensure that your code adheres to the project's coding standards and include tests for new features or bug fixes.

License

SecureProps is open-sourced software licensed under the MIT license.

ilicmiljan/secure-props 适用场景与选型建议

ilicmiljan/secure-props 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「encryption」 「rsa」 「aes」 「decryption」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ilicmiljan/secure-props 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ilicmiljan/secure-props 我们能提供哪些服务?
定制开发 / 二次开发

基于 ilicmiljan/secure-props 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 79
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-06