PHPackages                             afk11/phpasn1 - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. afk11/phpasn1

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

afk11/phpasn1
=============

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

1.3.1(11y ago)080MITPHPPHP &gt;=5.3.0

Since Feb 3Pushed 5y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (30)Used By (0)

PHPASN1
=======

[](#phpasn1)

[![Build Status](https://camo.githubusercontent.com/347da5c8a8ccbb10824dedef91c17890f2748f0e0ad15f7e7e359666e4c840d0/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6667726f7373652f50485041534e312e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/fgrosse/PHPASN1)[![PHP 7 ready](https://camo.githubusercontent.com/6946654dcfb8471bafeb1fec10d2b8b806f7c2b941343fd89fdea8ff75a64286/687474703a2f2f7068703772656164792e74696d6573706c696e7465722e63682f6667726f7373652f50485041534e312f62616467652e737667)](https://travis-ci.org/fgrosse/PHPASN1)[![Coverage Status](https://camo.githubusercontent.com/26fbd164902fb5f302b5387574af06bd166a67848608bb380b0881d0d07a5490/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6667726f7373652f50485041534e312f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/fgrosse/PHPASN1?branch=master)

[![Latest Stable Version](https://camo.githubusercontent.com/0602a629ddbb9ea134b8ef36f0afaed8987c33f0b3c3ce06c80d59331c7719b7/68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f762f737461626c652e706e67)](https://packagist.org/packages/fgrosse/phpasn1)[![Total Downloads](https://camo.githubusercontent.com/a34fb9d5f301b24025f78d4ae2522332b3f3671e204852b950236fac93340c40/68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f646f776e6c6f6164732e706e67)](https://packagist.org/packages/fgrosse/phpasn1)[![Latest Unstable Version](https://camo.githubusercontent.com/a35d40a5f3d5c1fc4f77a6eda8688fddf56bdcb34d3ad403493110773910401d/68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f762f756e737461626c652e706e67)](https://packagist.org/packages/fgrosse/phpasn1)[![License](https://camo.githubusercontent.com/6c52d94205f7a845f1ba55b89a2e6bbc6893e6a0d350d381c6788651113d2f28/68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f6c6963656e73652e706e67)](https://packagist.org/packages/fgrosse/phpasn1)

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 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](https://packagist.org/packages/fgrosse/phpasn1#1.5.2).

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 fgrosse/phpasn1
```

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/fgrosse/PHPASN1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/Universal/SequenceTest.php#L148-148), [here](https://github.com/fgrosse/PHPASN1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/Universal/SequenceTest.php#L121) and [here](https://github.com/fgrosse/PHPASN1/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/fgrosse/PHPASN1/tree/master/examples).

### How do I contribute?

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

If you found an issue or have a question submit a github issue with detailed information.

In case you already know what caused the issue and feel in the mood to fix it, your code contributions are always welcome. Just fork the repository, implement your changes and make sure that you covered everything with tests. Afterwards submit a pull request via github and be a little patient :) I usually try to comment and/or merge as soon as possible.

#### Mailing list

[](#mailing-list)

New features or questions can be discussed in [this google group/mailing list](https://groups.google.com/d/forum/phpasn1).

### Thanks

[](#thanks)

To [all contributors](https://github.com/fgrosse/PHPASN1/graphs/contributors) so far!

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 76.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 ~16 days

Total

6

Last Release

4038d ago

Major Versions

0.9.0 → 1.0.02015-02-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/763cdc5cc50414bacd4ff1080eede79432f7c3cc26a3d8aa9031b1537a8bd734?d=identicon)[thomaskerin](/maintainers/thomaskerin)

---

Top Contributors

[![fgrosse](https://avatars.githubusercontent.com/u/733004?v=4)](https://github.com/fgrosse "fgrosse (163 commits)")[![afk11](https://avatars.githubusercontent.com/u/5617245?v=4)](https://github.com/afk11 "afk11 (15 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)")[![Blaimi](https://avatars.githubusercontent.com/u/1222379?v=4)](https://github.com/Blaimi "Blaimi (3 commits)")[![devicenull](https://avatars.githubusercontent.com/u/58017?v=4)](https://github.com/devicenull "devicenull (1 commits)")[![oRastor](https://avatars.githubusercontent.com/u/1734394?v=4)](https://github.com/oRastor "oRastor (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)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/afk11-phpasn1/health.svg)

```
[![Health](https://phpackages.com/badges/afk11-phpasn1/health.svg)](https://phpackages.com/packages/afk11-phpasn1)
```

###  Alternatives

[beyondcode/laravel-comments

Add comments to your Laravel application

605414.2k2](/packages/beyondcode-laravel-comments)[graphp/graph

GraPHP is the mathematical graph/network library written in PHP.

711292.6k3](/packages/graphp-graph)[kucrut/vite-for-wp

Vite integration for WordPress plugins and themes development.

321127.6k6](/packages/kucrut-vite-for-wp)[cijic/phpmorphy

phpMorphy - morphological analyzer library for Russian, English, German and Ukrainian languages.

115339.9k7](/packages/cijic-phpmorphy)[alrik11es/cowsayphp

Cowsay port in PHP

72516.3k1](/packages/alrik11es-cowsayphp)[glhd/conveyor-belt

14797.0k](/packages/glhd-conveyor-belt)

PHPackages © 2026

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