v2 / vlib / x / encoding / asn1 / README.md
82 lines · 60 sloc · 2.31 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw

asn1

asn1 is a pure V Language module for X.690 Abstract Syntax Notation One (ASN.1) Distinguished Encoding Rules (DER) encoding and decoding.

This module provides you with the ability to generate and parse ASN.1 encoded data. More precisely, it provides you with the ability to generate and parse data encoded with ASN.1’s DER (Distinguished Encoding Rules) encoding. It does not support other than DER.

[!CAUTION] This module is marked as an experimental, so its subject to change (possibly rapidly). Use with caution, submit bugs when found, and please gives feedback.

Supported ASN.1 Type

Currently supports the following basic ASN1 types:

Features


Code Examples

Here are some simple usage examples.

import x.encoding.asn1

fn main() {
    value := asn1.Integer.from_int(32)

    output := asn1.encode(value)!
    assert output == [u8(0x02), 0x01, 0x20]

    // you can encode (serialize) with string options
    output2 := asn1.encode_with_options(value, 'context_specific:5;explicit;inner:2')!
    assert output2 == [u8(0xa5), 0x03, 0x02, 0x01, 0x20]

    // You can decode (deserialize) back the bytes into Element.
    el := asn1.decode_with_options([u8(0xa5), 0x03, 0x02, 0x01, 0x20],
        'context_specific:5;explicit;inner:2')!

    // el is an Element, turn it into underlying object
    int_el := el.into_object[asn1.Integer]()!

    int_el_value := int_el.as_i64()!
    assert int_el_value == 32
}

See more complete examples in the examples directory.

Documentation

See the documentation for more detailed information on how to use functionality in this module.

License

This project is licensed under the MIT License (see LICENSE file)