xepozz/php-age
Composer 安装命令:
composer require xepozz/php-age
包简介
README 文档
README
A pure PHP implementation of the age file encryption format, compatible with the age specification (v1).
Built entirely on PHP's ext-sodium (libsodium) — no external binary dependencies.
Features
- X25519 recipients — public-key encryption using Curve25519
- Scrypt passphrases — password-based encryption with configurable work factor
- Multiple recipients — encrypt a file for several public keys at once
- ASCII armor — PEM-style encoding/decoding
- Key generation — generate identity/recipient key pairs
- Cross-compatible — interoperable with age (Go), rage (Rust), typage (TypeScript), and other spec-compliant implementations
Requirements
- PHP 8.1+
ext-sodium(bundled with PHP since 7.2)
Installation
composer require xepozz/php-age
Usage
Encrypt with a recipient (public key)
use Xepozz\PhpAge\Encrypter; use Xepozz\PhpAge\Decrypter; $e = new Encrypter(); $e->addRecipient('age1tgyuvdlmpejqsdf847hevurz9szk7vf3j7ytfyqecgzvphvu2d8qrtaxl6'); $ciphertext = $e->encrypt('hello, world!'); $d = new Decrypter(); $d->addIdentity('AGE-SECRET-KEY-1RKH0DGHQ0FU6VLXX2VW6Y3W2TKK7KR4J36N9SNDXK75JHCJ3N6JQNZJF5J'); $plaintext = $d->decrypt($ciphertext); // "hello, world!"
Encrypt with a passphrase
use Xepozz\PhpAge\Encrypter; use Xepozz\PhpAge\Decrypter; $e = new Encrypter(); $e->setPassphrase('my-secret-passphrase'); $ciphertext = $e->encrypt('hello, world!'); $d = new Decrypter(); $d->addPassphrase('my-secret-passphrase'); $plaintext = $d->decrypt($ciphertext); // "hello, world!"
Generate a key pair
use Xepozz\PhpAge\Age; $identity = Age::generateIdentity(); // AGE-SECRET-KEY-1... $recipient = Age::identityToRecipient($identity); // age1...
Multiple recipients
use Xepozz\PhpAge\Encrypter; $e = new Encrypter(); $e->addRecipient('age1...'); $e->addRecipient('age1...'); $ciphertext = $e->encrypt('secret data');
ASCII armor
use Xepozz\PhpAge\Armor; $encoded = Armor::encode($binaryData); // -----BEGIN AGE ENCRYPTED FILE----- // ...base64... // -----END AGE ENCRYPTED FILE----- $decoded = Armor::decode($encoded);
Custom scrypt work factor
use Xepozz\PhpAge\Encrypter; $e = new Encrypter(); $e->setScryptWorkFactor(18); // default is 18 (2^18 = 262144 iterations) $e->setPassphrase('my-passphrase'); $ciphertext = $e->encrypt('data');
Testing
Run the full test suite:
vendor/bin/phpunit
Run fast tests only (excludes heavy scrypt RFC vectors):
vendor/bin/phpunit --exclude-group=slow
Run with code coverage:
XDEBUG_MODE=coverage vendor/bin/phpunit --exclude-group=slow --coverage-text
Code coverage
Code Coverage Report:
Summary:
Classes: 100.00% (13/13)
Methods: 100.00% (47/47)
Lines: 100.00% (465/465)
Xepozz\PhpAge\Age .................. Lines: 100.00%
Xepozz\PhpAge\Armor ............... Lines: 100.00%
Xepozz\PhpAge\Bech32 .............. Lines: 100.00%
Xepozz\PhpAge\Decrypter ........... Lines: 100.00%
Xepozz\PhpAge\Encrypter ........... Lines: 100.00%
Xepozz\PhpAge\Header .............. Lines: 100.00%
Xepozz\PhpAge\Scrypt .............. Lines: 100.00%
Xepozz\PhpAge\ScryptIdentity ...... Lines: 100.00%
Xepozz\PhpAge\ScryptRecipient ..... Lines: 100.00%
Xepozz\PhpAge\Stanza .............. Lines: 100.00%
Xepozz\PhpAge\Stream .............. Lines: 100.00%
Xepozz\PhpAge\X25519Identity ...... Lines: 100.00%
Xepozz\PhpAge\X25519Recipient ..... Lines: 100.00%
150 tests, 236 assertions — runs in ~0.4 seconds (excluding slow RFC scrypt vectors).
Specification and references
- age specification (v1) — the format this library implements
- age (Go) — the original reference implementation by Filippo Valsorda
- typage (TypeScript) — TypeScript implementation, used as the reference for this PHP port
- RFC 7914 — scrypt key derivation function
- RFC 7748 — X25519 key agreement
- RFC 8439 — ChaCha20-Poly1305 AEAD
License
MIT
xepozz/php-age 适用场景与选型建议
xepozz/php-age 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 xepozz/php-age 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xepozz/php-age 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2026-02-21