定制 initphp/encryption 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

initphp/encryption

Composer 安装命令:

composer require initphp/encryption

包简介

Secure, modern symmetric encryption for PHP built on top of OpenSSL and libsodium.

README 文档

README

Secure, modern symmetric encryption for PHP on top of OpenSSL and libsodium.

Latest Stable Version Total Downloads License PHP Version Require

Why

PHP's encryption primitives are powerful but unforgiving. Pick the wrong cipher, mix up IV and HMAC ordering, forget constant-time comparison, or hand libsodium a 14-byte "key" and you ship a vulnerability — or, more often, a silent failure that "works on my machine".

This package wraps ext-openssl and ext-sodium behind a small, opinionated API:

  • Authenticated by default. OpenSSL uses encrypt-then-MAC; Sodium uses the built-in AEAD construction.
  • Keys of any non-empty length are accepted and derived to the size the primitive actually requires.
  • Ciphertexts are self-describing: a 2-byte header (version + serializer flag) lets the library reject malformed or out-of-date input with a clear error instead of returning garbage.
  • A single EncryptionException covers every failure mode, so a try / catch is enough to handle all error paths.
  • JSON is the default payload serializer, so the historical unserialize()-on-attacker-controlled-bytes pitfall is closed by default.

Requirements

  • PHP 8.1 or higher
  • ext-openssl for the OpenSSL handler
  • ext-sodium for the Sodium handler

Both extensions ship with mainstream PHP distributions, but the package only loads the one you actually instantiate — you can use one handler without the other being available.

Installation

composer require initphp/encryption

Quickstart

<?php

require __DIR__ . '/vendor/autoload.php';

use InitPHP\Encryption\Encrypt;
use InitPHP\Encryption\OpenSSL;

$handler = Encrypt::use(OpenSSL::class, [
    'key' => getenv('APP_ENCRYPTION_KEY'),
]);

$ciphertext = $handler->encrypt(['user_id' => 42, 'role' => 'admin']);
// → "02006f1c…": hex string, safe to store in cookies / DBs / URL params

$plaintext = $handler->decrypt($ciphertext);
// → ['user_id' => 42, 'role' => 'admin']

The Sodium handler has the same surface:

use InitPHP\Encryption\Encrypt;
use InitPHP\Encryption\Sodium;

$handler = Encrypt::use(Sodium::class, [
    'key' => getenv('APP_ENCRYPTION_KEY'),
]);

$ciphertext = $handler->encrypt('a secret message');
$plaintext  = $handler->decrypt($ciphertext);

Configuration

Every option is optional except key. Unknown keys are ignored. Keys are case-insensitive on input ('CIPHER' and 'cipher' are the same option).

Option Used by Default Description
key both required The user-supplied secret. Any non-empty string; the handler derives a key of the correct length internally.
cipher OpenSSL AES-256-CTR Any algorithm from openssl_get_cipher_methods().
algo OpenSSL SHA256 Any algorithm from hash_hmac_algos(). Used both for HKDF key derivation and for the HMAC tag.
blocksize Sodium 16 Block size for sodium_pad() / sodium_unpad(). Must be a positive integer.
serializer both 'json' One of 'json', 'php_serialize', 'php', 'serialize'. See Serialization.

Options can be set in three places, in order of precedence (highest wins):

// 1) Per-call override
$handler->encrypt($data, ['cipher' => 'AES-256-GCM']);

// 2) Mutated on the handler
$handler->setOption('cipher', 'AES-256-GCM');
$handler->setOptions(['cipher' => 'AES-256-GCM', 'algo' => 'SHA512']);

// 3) Constructor / factory
$handler = Encrypt::use(OpenSSL::class, ['cipher' => 'AES-256-GCM']);

Per-call options do not mutate the handler — they are merged into a fresh array for that single call only.

Serialization

encrypt() accepts mixed and round-trips the value through a serializer chosen via the serializer option. The flag is embedded in the ciphertext, so decrypt() always restores the original type without you having to track the choice yourself.

serializer value On-the-wire flag Behaviour
'json' (default) 0x00 Uses json_encode/json_decode with JSON_THROW_ON_ERROR. Safe: no PHP class is ever instantiated during decoding. Cannot carry raw binary bytes — use php_serialize if you need that.
'php_serialize', 'php', 'serialize' 0x01 Uses serialize()/unserialize() with ['allowed_classes' => false]. Round-trips scalars, arrays and binary strings; custom objects degrade to __PHP_Incomplete_Class on decode.

The PHP serializer is opt-in for one reason only: even though we always pass allowed_classes:false, the safer default lets you not have to think about object-injection vectors at all.

Writing a Custom Handler

Extend BaseHandler (not OpenSSL / Sodium — those are final) and implement encrypt() and decrypt():

namespace App\Crypto;

use InitPHP\Encryption\BaseHandler;
use InitPHP\Encryption\Exceptions\EncryptionException;

final class MyHandler extends BaseHandler
{
    public function encrypt(mixed $data, array $options = []): string
    {
        $options       = $this->resolveOptions($options);
        $key           = $this->requireKey($options);
        $serializerFlag = $this->serializerFlag($options);

        $payload = $this->serializePayload($data, $serializerFlag);
        // ... apply your primitive of choice, return a hex-encoded string ...
    }

    public function decrypt(string $data, array $options = []): mixed
    {
        $options       = $this->resolveOptions($options);
        $key           = $this->requireKey($options);

        // ... reverse the encoding, return $this->unserializePayload($plain, $flag) ...
    }
}

// Use it via the factory just like the built-in handlers:
$handler = \InitPHP\Encryption\Encrypt::use(\App\Crypto\MyHandler::class, [
    'key' => 'secret',
]);

BaseHandler gives you resolveOptions(), requireKey(), serializerFlag(), serializePayload() and unserializePayload() for free, so you only write the cryptographic glue.

Error Handling

Every failure path raises InitPHP\Encryption\Exceptions\EncryptionException (which extends \RuntimeException). A single catch covers everything:

use InitPHP\Encryption\Exceptions\EncryptionException;

try {
    $plaintext = $handler->decrypt($incoming);
} catch (EncryptionException $e) {
    // Bad input, tampered ciphertext, missing key, unsupported format
    // version, unknown cipher or hash algorithm, …
    $logger->warning('decrypt failed', ['reason' => $e->getMessage()]);
}

Notable messages you may see:

  • Unsupported ciphertext format version 0x01; expected 0x02. Ciphertexts produced by 1.x are not readable by 2.x.
  • HMAC verification failed; ciphertext is corrupted or has been tampered with.
  • Sodium decryption failed; ciphertext is corrupted or has been tampered with.
  • The "key" option is required and must be a non-empty string.
  • Unknown OpenSSL cipher "…".

Security Notes

  • Key management is your job. Store the key outside the code repository — environment variable, secret manager, KMS, etc. — and rotate it like any other secret.
  • Key strength matters. The handler accepts any non-empty user key and derives one of the right length, but it cannot add entropy that the input does not contain. Use a random 256-bit string (bin2hex(random_bytes(32))) in production rather than a passphrase.
  • Authentication is always on. OpenSSL ciphertexts include an HMAC of the header, IV, and ciphertext; Sodium uses its built-in AEAD. There is no "encrypt without authenticate" mode.
  • Format is versioned. The first byte of every ciphertext identifies the format. A future major release that changes the layout will bump this byte and reject older ciphertexts with a clear error.
  • Found something concerning? See SECURITY.md — please do not open a public issue for vulnerabilities.

Upgrading from 1.x

Version 2.0 is a hard reset of the public surface and the on-wire format:

  • Minimum PHP version is now 8.1.
  • Ciphertexts produced by 1.x cannot be decrypted by 2.x. Plan a re-encryption migration before upgrading.
  • The default payload serializer is JSON (was serialize). Pass 'serializer' => 'php_serialize' to keep the old behaviour.
  • Encrypt::create() has been removed; use Encrypt::use().
  • The Sodium handler no longer requires a 32-byte key — any non-empty string is now accepted and derived internally.
  • ext-mbstring is no longer required.

A full migration walk-through lives in docs/08-migration-v1-to-v2.md (shipped with the package; see also the docs/ index).

Contributing

PRs are welcome. Please read CONTRIBUTING.md first — it covers the local quality gates (composer test, composer phpstan, composer cs-check) and the security-review process for changes touching the cryptographic primitives.

License

MIT — see LICENSE.

initphp/encryption 适用场景与选型建议

initphp/encryption 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 242 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 initphp/encryption 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-15