legalario / phpasn1
一个PHP框架,允许您使用ITU-T X.690编码规则对任意ASN.1结构进行编码和解码。
Requires
- php: ~7.1.0 || ~7.2.0 || ~7.3.0 || ~7.4.0 || ~8.0.0 || ~8.1.0
Requires (Dev)
- php-coveralls/php-coveralls: ~2.0
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
Suggests
- ext-bcmath: BCmath is the fallback extension for big integer calculations
- ext-curl: For loading OID information from the web if they have not bee defined statically
- ext-gmp: GMP is the preferred extension for big integer calculations
- phpseclib/bcmath_compat: BCmath polyfill for servers where neither GMP nor BCmath is available
This package is auto-updated.
Last update: 2024-09-15 23:34:11 UTC
README
一个PHP框架,允许您使用ASN.1结构编码和解码ITU-T X.690编码规则。这种编码在X.509 PKI环境或异构计算机系统之间的通信中非常常用。
API允许您将ASN.1结构编码为二进制数据,例如证书签名请求(CSR)、X.509证书或证书吊销列表(CRL)。PHPASN1还可以将BER编码的二进制数据读取到由用户操作并在之后重新编码的独立的PHP对象中。
现在可以在CHANGELOG.md中找到changelog。
依赖关系
PHPASN1需要至少PHP 7.0
和gmp
或bcmath
扩展之一。从v2.0
开始不再支持旧版本的PHP(即PHP 5.6)。如果您必须使用过时的PHP版本,请考虑使用PHPASN v1.5。
为了直接从网络上加载对象标识符名称,使用curl。
安装
安装此库的首选方法是依靠Composer。
$ composer require legalario/phpasn1
用法
编码ASN.1结构
PHPASN1为每个实现的ASN.1通用类型提供了一个类。构造函数应该是相当自解释的,所以您应该不会有太大麻烦开始。所有数据都将使用DER编码进行编码。
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);
解码二进制数据
解码BER编码的二进制数据与编码一样简单。
use FG\ASN1\ASNObject; $base64String = ... $binaryData = base64_decode($base64String); $asnObject = ASNObject::fromBinary($binaryData); // do stuff
如果您已经确切知道预期数据应该是什么样子,可以使用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);
您可以使用此函数确保您的数据具有您所期望的格式。
导航解码数据
所有构造的类(即Sequence
和Set
)都可以通过数组访问或使用迭代器进行导航。您可以在此处找到示例:此处、此处和此处。
给我更多示例!
要查看API类的示例用法或生成的输出,请查看示例。
如何贡献?
如果您发现了问题或有问题,请提交带有详细信息的github问题。
如果您已经知道问题的原因并且愿意修复它,您的代码贡献总是受欢迎。只需分支存储库,实现您的更改,并确保您已经用测试覆盖了所有内容。然后通过github提交拉取请求,并稍加耐心:我通常会尽快评论和/或合并。
邮件列表
新功能或问题可以在这个谷歌群组/邮件列表中讨论。
谢谢
感谢到目前为止的所有贡献者!
许可证
此库遵循MIT许可证进行分发。