Giter Site home page Giter Site logo

asn1's Introduction

FreeDSx ASN1 codecov

FreeDSx ASN1 is a PHP library for dealing with ASN.1 data structures. Its original focus was on ASN.1 BER encoding used in LDAP as part of the FreeDSx LDAP library. It was moved to its own library to allow for additional encoders and reuse in other projects.

Getting Started

Install via composer:

composer require freedsx/asn1

Encoding

To encode an ASN.1 structure you can use the helper methods of the Asn1 class and an encoder:

use FreeDSx\Asn1\Asn1;
use FreeDSx\Asn1\Encoders;

# Create the ASN.1 structure you need...
$asn1 = Asn1::sequence(
    Asn1::integer(9999),
    Asn1::octetString('foo'),
    Asn1::boolean(true)
);

# Encoded $bytes will now contain the BER binary representation of a sequence containing:
#  - An integer type of value 9999
#  - An octet string type of value 'foo'
#  - A boolean type of true
$bytes = Encoders::ber()->encode($asn1);

# Encode using the more strict DER encoder
$bytes = Encoders::der()->encode($asn1);

Decoding

To decode an ASN.1 structure you can get an encoder and call decode then parse it out:

use FreeDSx\Asn1\Asn1;
use FreeDSx\Asn1\Encoders;
use FreeDSx\Asn1\Type\SequenceType;
use FreeDSx\Asn1\Type\OctetStringType;
use FreeDSx\Asn1\Type\IntegerType;
use FreeDSx\Asn1\Type\BooleanType;

# Assuming bytes contains the binary BER encoded sequence described in the encoding section
# Get a BER encoder instance, call decode on it, and $pdu will now be a sequence object.
$pdu = Encoders::ber()->decode($bytes);

# You could also decode using DER, if that's what you're expecting...
$pdu = Encoders::der()->decode($bytes);

# Validate the structure you are expecting...
if (!($pdu instanceof SequenceType && count($pdu) === 3)) {
    echo "Decoded structure is invalid.".PHP_EOL;
    exit;
}

# Loop through the sequence and check the individual types it contains...
foreach ($pdu as $i => $type) {
    if ($i === 0 && $type instanceof IntegerType) {
        var_dump($type->getValue());
    } elseif ($i === 1 && $type instanceof OctetStringType) {
        var_dump($type->getValue());
    } elseif ($i === 2 && $type instanceof BooleanType) {
        var_dump($type->getValue());
    } else {
        echo "Invalid type encountered.".PHP_EOL;
        exit;
    }
}

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.