Git Product home page Git Product logo

asn1's Introduction

Build Status Scrutinizer Code Quality Coverage Status License

ASN.1

A PHP library for X.690 Abstract Syntax Notation One (ASN.1) Distinguished Encoding Rules (DER) encoding and decoding.

Installation

This library is available on Packagist.

composer require sop/asn1

Usage

The general idea is that each ASN.1 type has its corresponding PHP class, that knows the details of encoding and decoding the specific type.

To decode DER data, use fromDER static method of the expected type. To encode object to DER, use toDER instance method.

All objects are immutable and method chaining is promoted for the fluency of the API. Exception shall be thrown on errors.

Code Examples

Here are some simple usage examples. Namespaces are omitted for brevity.

Encode

Encode a sequence containing a UTF-8 string, an integer and an explicitly tagged object identifier.

$seq = new Sequence(
    new UTF8String("Hello"),
    new Integer(42),
    new ExplicitlyTaggedType(
        1, new ObjectIdentifier("1.3.6.1.3"))
);
$der = $seq->toDER();
echo bin2hex($der);

Outputs:

30120c0548656c6c6f02012aa10606042b060103

Decode

Decode DER encoding from above.

$seq = Sequence::fromDER($der);
echo $seq->at(0)->asUTF8String()->string() . "\n";
echo $seq->at(1)->asInteger()->number() . "\n";
echo $seq->at(2)->asTagged()->asExplicit()
    ->asObjectIdentifier()->oid() . "\n";

Outputs:

Hello
42
1.3.6.1.3

Real-World Examples

See the following for more practical real-world usage examples.

License

This project is licensed under the MIT License.

asn1's People

Contributors

sop avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.