PHPackages                             genkgo/php-asn1 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. genkgo/php-asn1

ActiveLibrary

genkgo/php-asn1
===============

A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.

v2.9.0(4mo ago)4685.0k—2.6%5[1 PRs](https://github.com/genkgo/php-asn1/pulls)2MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Feb 3Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/genkgo/php-asn1)[ Packagist](https://packagist.org/packages/genkgo/php-asn1)[ Docs](https://github.com/FGrosse/PHPASN1)[ RSS](/packages/genkgo-php-asn1/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (35)Used By (2)

PHPASN1
=======

[](#phpasn1)

[![Build Status](https://github.com/genkgo/php-asn1/actions/workflows/phpunit.yml/badge.svg)](https://github.com/genkgo/php-asn1/actions/workflows/phpunit.yml)
---------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#)

A PHP Framework that allows you to encode and decode arbitrary [ASN.1](http://www.itu.int/ITU-T/asn1/) structures using the [ITU-T X.690 Encoding Rules](http://www.itu.int/ITU-T/recommendations/rec.aspx?rec=x.690). This encoding is very frequently used in [X.509 PKI environments](http://en.wikipedia.org/wiki/X.509) 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](http://en.wikipedia.org/wiki/X.690#BER_encoding) 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](CHANGELOG.md).

Dependencies
------------

[](#dependencies)

PHPASN1 requires at [a PHP versions receiving security support](https://www.php.net/supported-versions.php) and either the `gmp` or `bcmath` extension.

Support for older PHP versions:

- For PHP version 5 use `v1.x`.
- For PHP version 7 use `v2.5.x`.

For the loading of object identifier names directly from the web [curl](http://php.net/manual/en/book.curl.php) is used.

Installation
------------

[](#installation)

The preferred way to install this library is to rely on [Composer](https://getcomposer.org/):

```
$ composer require genkgo/php-asn1
```

Usage
-----

[](#usage)

### Encoding ASN.1 Structures

[](#encoding-asn1-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](http://en.wikipedia.org/wiki/X.690#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-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

[](#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](https://github.com/genkgo/php-asn1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/Universal/SequenceTest.php#L148-148), [here](https://github.com/genkgo/php-asn1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/Universal/SequenceTest.php#L121) and [here](https://github.com/genkgo/php-asn1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/TemplateParserTest.php#L45).

### Give me more examples!

[](#give-me-more-examples)

To see some example usage of the API classes or some generated output check out the [examples](https://github.com/genkgo/php-asn1/tree/master/examples).

Contributing
------------

[](#contributing)

### How do I contribute?

[](#how-do-i-contribute)

This project is no longer maintained and thus does not accept any new contributions.

### Historical contributors

[](#historical-contributors)

Huge thanks to Friedrich Große. He maintained this library for 11 years. And of course to [all contributors](https://github.com/genkgo/php-asn1/graphs/contributors) so far!

License
-------

[](#license)

This library is distributed under the [MIT License](LICENSE).

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance76

Regular maintenance activity

Popularity44

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity92

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 63.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~137 days

Recently: every ~252 days

Total

30

Last Release

132d ago

Major Versions

0.9.0 → 1.0.02015-02-03

1.5.2 → 2.0.02017-09-17

1.5.3 → 2.0.12017-12-17

v1.5.4 → v2.0.22018-12-02

PHP version history (9 changes)0.9.0PHP &gt;=5.3.0

1.4.0PHP &gt;=5.6.0

2.0.0PHP &gt;=7.0.0

v2.4.0PHP ~7.1.0 || ~7.2.0 || ~7.3.0 || ~7.4.0 || ~8.0.0 || ~8.1.0

v2.5.0PHP ^7.1 || ^8.0

v2.6.0PHP ~8.0.0 || ~8.1.0 || ~8.2.0

v2.7.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

v2.8.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

v2.9.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9d827a1c73491b0599e518a1a1bac8aa5cb07a4e3a68fcf90bfd05959ae6d302?d=identicon)[frederikbosch](/maintainers/frederikbosch)

---

Top Contributors

[![fgrosse](https://avatars.githubusercontent.com/u/733004?v=4)](https://github.com/fgrosse "fgrosse (184 commits)")[![frederikbosch](https://avatars.githubusercontent.com/u/1552577?v=4)](https://github.com/frederikbosch "frederikbosch (25 commits)")[![afk11](https://avatars.githubusercontent.com/u/5617245?v=4)](https://github.com/afk11 "afk11 (16 commits)")[![TimWolla](https://avatars.githubusercontent.com/u/209270?v=4)](https://github.com/TimWolla "TimWolla (14 commits)")[![nathanntg](https://avatars.githubusercontent.com/u/194728?v=4)](https://github.com/nathanntg "nathanntg (12 commits)")[![kgilden](https://avatars.githubusercontent.com/u/918599?v=4)](https://github.com/kgilden "kgilden (10 commits)")[![Naugrimm](https://avatars.githubusercontent.com/u/5753604?v=4)](https://github.com/Naugrimm "Naugrimm (5 commits)")[![sanderkruger](https://avatars.githubusercontent.com/u/1422536?v=4)](https://github.com/sanderkruger "sanderkruger (4 commits)")[![arokettu](https://avatars.githubusercontent.com/u/963485?v=4)](https://github.com/arokettu "arokettu (3 commits)")[![Blaimi](https://avatars.githubusercontent.com/u/1222379?v=4)](https://github.com/Blaimi "Blaimi (3 commits)")[![paragonie-security](https://avatars.githubusercontent.com/u/15914520?v=4)](https://github.com/paragonie-security "paragonie-security (3 commits)")[![herndlm](https://avatars.githubusercontent.com/u/5738896?v=4)](https://github.com/herndlm "herndlm (1 commits)")[![bilogic](https://avatars.githubusercontent.com/u/946010?v=4)](https://github.com/bilogic "bilogic (1 commits)")[![oRastor](https://avatars.githubusercontent.com/u/1734394?v=4)](https://github.com/oRastor "oRastor (1 commits)")[![devicenull](https://avatars.githubusercontent.com/u/58017?v=4)](https://github.com/devicenull "devicenull (1 commits)")[![PhilETaylor](https://avatars.githubusercontent.com/u/400092?v=4)](https://github.com/PhilETaylor "PhilETaylor (1 commits)")[![f1mishutka](https://avatars.githubusercontent.com/u/24731190?v=4)](https://github.com/f1mishutka "f1mishutka (1 commits)")[![staabm](https://avatars.githubusercontent.com/u/120441?v=4)](https://github.com/staabm "staabm (1 commits)")[![Tatikoma](https://avatars.githubusercontent.com/u/1888609?v=4)](https://github.com/Tatikoma "Tatikoma (1 commits)")[![lcts](https://avatars.githubusercontent.com/u/6643758?v=4)](https://github.com/lcts "lcts (1 commits)")

---

Tags

x509asn1encodingDERx690binarydecodingber

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/genkgo-php-asn1/health.svg)

```
[![Health](https://phpackages.com/badges/genkgo-php-asn1/health.svg)](https://phpackages.com/packages/genkgo-php-asn1)
```

###  Alternatives

[phpseclib/phpseclib

PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.

5.6k434.8M1.3k](/packages/phpseclib-phpseclib)[falseclock/advanced-cms

A PHP Library that allows you to decode and manipulate CAdES or in other words CMS Advanced Electronic Signatures described in ETSI standart TS 101 733.

223.2k](/packages/falseclock-advanced-cms)[adapik/cms

A PHP Library that allows you to decode ASN.1 CMS using Basic Encoding Rules (BER).

147.2k1](/packages/adapik-cms)[spomky-labs/pki-framework

A PHP framework for managing Public Key Infrastructures. It comprises X.509 public key certificates, attribute certificates, certification requests and certification path validation.

2725.9M11](/packages/spomky-labs-pki-framework)[kelunik/certificate

Access certificate details and transform between different formats.

10938.3M8](/packages/kelunik-certificate)[sop/asn1

A PHP library for X.690 ASN.1 DER encoding and decoding.

601.7M39](/packages/sop-asn1)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
