fgrosse/phpasn1
最新稳定版本:v2.5.0
Composer 安装命令:
composer require fgrosse/phpasn1
包简介
A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.
README 文档
README
Notice: This library is no longer actively maintained!
If you are currently using PHPASN1, this might not be an immediate problem for you, since this library was always rather stable.However, you are advised to migrate to alternative packages to ensure that your applications remain functional also with newer PHP versions.
A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules. This encoding is very frequently used in X.509 PKI environments or the communication between heterogeneous computer systems.
The API allows you to encode ASN.1 structures to create binary data such as certificate signing requests (CSR), X.509 certificates or certificate revocation lists (CRL). PHPASN1 can also read BER encoded binary data into separate PHP objects that can be manipulated by the user and reencoded afterwards.
The changelog can now be found at CHANGELOG.md.
Dependencies
PHPASN1 requires at least PHP 7.0 and either the gmp or bcmath extension.
Support for older PHP versions (i.e. PHP 5.6) was dropped starting with v2.0.
If you must use an outdated PHP version consider using PHPASN v1.5.
For the loading of object identifier names directly from the web curl is used.
Installation
The preferred way to install this library is to rely on Composer:
$ composer require fgrosse/phpasn1
Usage
Encoding ASN.1 Structures
PHPASN1 offers you a class for each of the implemented ASN.1 universal types. The constructors should be pretty self explanatory so you should have no big trouble getting started. All data will be encoded using DER encoding
use FG\ASN1\OID; use FG\ASN1\Universal\Integer; use FG\ASN1\Universal\Boolean; use FG\ASN1\Universal\Enumerated; use FG\ASN1\Universal\IA5String; use FG\ASN1\Universal\ObjectIdentifier; use FG\ASN1\Universal\PrintableString; use FG\ASN1\Universal\Sequence; use FG\ASN1\Universal\Set; use FG\ASN1\Universal\NullObject; $integer = new Integer(123456); $boolean = new Boolean(true); $enum = new Enumerated(1); $ia5String = new IA5String('Hello world'); $asnNull = new NullObject(); $objectIdentifier1 = new ObjectIdentifier('1.2.250.1.16.9'); $objectIdentifier2 = new ObjectIdentifier(OID::RSA_ENCRYPTION); $printableString = new PrintableString('Foo bar'); $sequence = new Sequence($integer, $boolean, $enum, $ia5String); $set = new Set($sequence, $asnNull, $objectIdentifier1, $objectIdentifier2, $printableString); $myBinary = $sequence->getBinary(); $myBinary .= $set->getBinary(); echo base64_encode($myBinary);
Decoding binary data
Decoding BER encoded binary data is just as easy as encoding it:
use FG\ASN1\ASNObject; $base64String = ... $binaryData = base64_decode($base64String); $asnObject = ASNObject::fromBinary($binaryData); // do stuff
If you already know exactly how your expected data should look like you can use the FG\ASN1\TemplateParser:
use FG\ASN1\TemplateParser; // first define your template $template = [ Identifier::SEQUENCE => [ Identifier::SET => [ Identifier::OBJECT_IDENTIFIER, Identifier::SEQUENCE => [ Identifier::INTEGER, Identifier::BITSTRING, ] ] ] ]; // if your binary data is not matching the template you provided this will throw an `\Exception`: $parser = new TemplateParser(); $object = $parser->parseBinary($data, $template); // there is also a convenience function if you parse binary data from base64: $object = $parser->parseBase64($data, $template);
You can use this function to make sure your data has exactly the format you are expecting.
Navigating decoded data
All constructed classes (i.e. Sequence and Set) can be navigated by array access or using an iterator.
You can find examples
here,
here and
here.
Give me more examples!
To see some example usage of the API classes or some generated output check out the examples.
How do I contribute?
This project is no longer maintained and thus does not accept any new contributions.
Thanks
To all contributors so far!
License
This library is distributed under the MIT License.
fgrosse/phpasn1 适用场景与选型建议
fgrosse/phpasn1 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.88M 次下载、GitHub Stars 达 173, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「encoding」 「x509」 「x.509」 「asn1」 「asn.1」 「binary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fgrosse/phpasn1 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fgrosse/phpasn1 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fgrosse/phpasn1 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
integration of the Panda encoding services into the Symfony2 Framework
FileBundle manages uploads and manipulations of files for the Kitpages CMS Bundle
A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.
A PHP Library that allows you to decode ASN.1 CMS using Basic Encoding Rules (BER).
Simple encoding/decoding of data and hash validation using a shared secret
^B A dead-simple PHP library to add caret notation in order to safely show strings that contain ASCII control characters (unprintable characters)
统计信息
- 总下载量: 33.88M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 175
- 点击次数: 41
- 依赖项目数: 31
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04