PHPackages                             ricco381/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. ricco381/phpasn1

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

ricco381/phpasn1
================

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

0.2(7y ago)0381MITPHPPHP &gt;=7.0.0

Since Apr 19Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ricco381/phpasn1)[ Packagist](https://packagist.org/packages/ricco381/phpasn1)[ Docs](https://github.com/FGRrosse/PHPASN1)[ RSS](/packages/ricco381-phpasn1/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (1)

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 FGR\ASN1\OID;
use FGR\ASN1\Universal\Integer;
use FGR\ASN1\Universal\Boolean;
use FGR\ASN1\Universal\Enumerated;
use FGR\ASN1\Universal\IA5String;
use FGR\ASN1\Universal\ObjectIdentifier;
use FGR\ASN1\Universal\PrintableString;
use FGR\ASN1\Universal\Sequence;
use FGR\ASN1\Universal\Set;
use FGR\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 FGR\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 `FGR\ASN1\TemplateParser`:

```
use FGR\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

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

2583d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/90cbdcd8dacd40c8a1f1f344051d5c6b1fd35cc9696f8de9f8a99e687ea3ec52?d=identicon)[ricco381](/maintainers/ricco381)

---

Top Contributors

[![ricco381](https://avatars.githubusercontent.com/u/11959563?v=4)](https://github.com/ricco381 "ricco381 (2 commits)")

---

Tags

x509asn1encodingDERx690binarydecodingber

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[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)[paragonie/constant_time_encoding

Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)

901329.7M148](/packages/paragonie-constant-time-encoding)[adapik/cms

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

147.2k1](/packages/adapik-cms)[kelunik/certificate

Access certificate details and transform between different formats.

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

An ASN.1 library for PHP.

15216.5k9](/packages/freedsx-asn1)[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)

PHPackages © 2026

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