tuzelko/yii2-key-storage
Composer 安装命令:
composer require tuzelko/yii2-key-storage
包简介
Named cryptographic key storage with format validation for Yii2 framework
README 文档
README
Named cryptographic key storage for the Yii2 framework.
Solves one recurring problem: "give me the raw bytes of key X, taken from env, decoded and validated — and fail loudly at the first use if the key is missing or malformed". Encryption keys, signing keys, HMAC secrets, TOTP seeds — all behind one named registry instead of ad-hoc base64_decode(getenv(...)) calls scattered around the codebase.
Features
- Named keys — one registry, keys referenced by name
- Text sources —
base64(standard and url-safe alphabets) orhex, typically from env - Modular key types — each format is a small class validating the decoded bytes; ship your own by implementing one interface
- Fail-fast validation — wrong length, bad encoding, missing value →
InvalidKeyExceptionwith the key name in the message - Interface-first — consumers depend on
KeyProviderInterface, not on the concrete storage - Memoization — decode and validation happen once per key per instance
- No ext requirements — key-type byte lengths are hardcoded, so describing sodium keys does not require ext-sodium
Requirements
- PHP >= 8.0
- yiisoft/yii2 ~2.0
Installation
composer require tuzelko/yii2-key-storage
Quick start
Register the storage in the DI container under the interface, so consumers never know the concrete class:
// config/main.php use tuzelko\yii\keystorage\KeyProviderInterface; use tuzelko\yii\keystorage\KeyStorage; use tuzelko\yii\keystorage\types\SodiumSecretboxKey; 'container' => [ 'singletons' => [ KeyProviderInterface::class => static fn () => new KeyStorage([ 'keys' => [ 'appCrypto' => [ 'base64' => getenv('APP_CRYPTO_KEY'), 'type' => SodiumSecretboxKey::class, ], ], ]), ], ],
$rawKey = Yii::$container->get(KeyProviderInterface::class)->getRaw('appCrypto');
getRaw() returns raw binary bytes, ready for sodium_* / hash_hmac / openssl_* calls.
Key configuration
Each entry under keys is name => descriptor. A descriptor has exactly one source encoding and a mandatory type:
| Field | Description |
|---|---|
base64 |
Base64-encoded key value (standard and url-safe alphabets accepted) |
hex |
Hex-encoded key value |
type |
KeyTypeInterface class name or instance — validates the decoded bytes |
'keys' => [ 'appCrypto' => ['base64' => getenv('CRYPTO_KEY'), 'type' => SodiumSecretboxKey::class], 'requestSigning' => ['hex' => getenv('SIGNING_KEY_HEX'), 'type' => Ed25519SecretKey::class], 'legacyAes' => ['base64' => getenv('LEGACY_KEY'), 'type' => new CustomLengthKey(16)], ],
Bundled key types
| Type | Valid length | For |
|---|---|---|
SodiumSecretboxKey |
32 bytes | sodium_crypto_secretbox (XSalsa20-Poly1305) |
Ed25519PublicKey |
32 bytes | sodium_crypto_sign verification |
Ed25519SecretKey |
64 bytes | sodium_crypto_sign signing |
CustomLengthKey(n) |
n bytes |
any fixed-length format not shipped with the package |
Custom key types
A key type is any class implementing KeyTypeInterface — one method, full control over what "valid" means:
use tuzelko\yii\keystorage\InvalidKeyException; use tuzelko\yii\keystorage\types\KeyTypeInterface; class PemRsaPrivateKey implements KeyTypeInterface { public function validate(string $raw): void { if (openssl_pkey_get_private($raw) === false) { throw new InvalidKeyException('not a valid PEM RSA private key.'); } } }
For plain length checks extend FixedLengthKey instead and implement only length(): int.
Docker secrets
Keys are always supplied as text via env. If you use Docker secrets, deliver them to env in your entrypoint (the common *_FILE pattern) — the storage intentionally does not read files, so there is exactly one secret-loading mechanism and no binary-vs-text file pitfalls.
Error handling
Every failure throws tuzelko\yii\keystorage\InvalidKeyException (extends RuntimeException) with the key name in the message:
| Condition | Message |
|---|---|
| Unknown key name | Key "x" is not configured. |
| Empty / unset env value | Key "x": base64 value is empty or not set. |
| Bad encoding | Key "x": invalid base64 string. / invalid hex string. |
| Missing source | Key "x" must specify one of: base64, hex. |
| Missing / wrong type | Key "x" has unknown or missing type; ... |
| Failed type validation | Key "x": wrong length: expected 32 bytes, got 16. |
Running tests
make test
Tests run inside Docker (PHP 8.3) with no local setup required.
License
MIT — see LICENSE.
tuzelko/yii2-key-storage 适用场景与选型建议
tuzelko/yii2-key-storage 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「extension」 「crypto」 「key」 「yii2」 「secrets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tuzelko/yii2-key-storage 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tuzelko/yii2-key-storage 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tuzelko/yii2-key-storage 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Provide a way to secure accesses to all routes of an symfony application.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 99
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 37
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-10