首页

源码搜藏网

首页 > 开发教程 > Web前端 >

ASN1.js是一个纯JavaScript库实现一个完整的BER ASN.1解码器

创建时间:2016-02-17 09:08  

抽象语法符号(ASN.1)是一种标准,符号描述来表示,编码,传输,以及电信和计算机网络解码数据的规则和结构。ASN1js是一个纯JavaScript库,实施这个标准。ASN.1是在网络上使用的所有的X.509相关的数据结构和许多其他协议的基础。

介绍

ASN1js是第一个库BER编码/ Javascript中设计的浏览器使用的解码。BER是基本编码规则的ASN.1,所有的人都是基于,DER是PKI应用程序使用的编码规则-它的一个子集BERASN1js库对测试免费提供ASN.1:2008测试套件,与相 关的JavaScript语言一定的局限性。

图书馆的特点

例子

    //#地区如何创建新的ASN。
     var sequence = new org.pkijs.asn1.SEQUENCE();
    sequence.value_block.value.push(new org.pkijs.asn1.INTEGER({ value: 1 }));

    var sequence_buffer = sequence.toBER(false); // Encode current sequence to BER (in ArrayBuffer)
    var current_size = sequence_buffer.byteLength;

    var integer_data = new ArrayBuffer(8);
    var integer_view = new Uint8Array(integer_data);
    integer_view[0] = 0x01;
    integer_view[1] = 0x01;
    integer_view[2] = 0x01;
    integer_view[3] = 0x01;
    integer_view[4] = 0x01;
    integer_view[5] = 0x01;
    integer_view[6] = 0x01;
    integer_view[7] = 0x01;

    sequence.value_block.value.push(new org.pkijs.asn1.INTEGER({
        is_hex_only: true,
        value_hex: integer_data
    })); // Put too long for decoding INTEGER value

    sequence_buffer = sequence.toBER(false);
    current_size = sequence_buffer.byteLength;
    // #endregion 
    // #region How to create new ASN.1 structures by calling constuctors with parameters 
    var sequence2 = new org.pkijs.asn1.SEQUENCE({
        value: [
            new org.pkijs.asn1.INTEGER({ value: 1 }),
            new org.pkijs.asn1.INTEGER({
                is_hex_only: true,
                value_hex: integer_data
            }),
        ]
    });
    // #endregion 
    // #region How to validate ASN.1 against pre-defined schema 
    var asn1_schema = new org.pkijs.asn1.SEQUENCE({
        name: "block1",
        value: [
            new org.pkijs.asn1.NULL({
                name: "block2"
            }),
            new org.pkijs.asn1.INTEGER({
                name: "block3",
                optional: true // This block is absent inside data, but it's "optional". Hence verification against the schema will be passed.
            })
        ]
    });
    // #endregion

    var variant1 = org.pkijs.verifySchema(encoded_sequence, asn1_schema); // Verify schema together with decoding of raw data
    var variant1_verified = variant1.verified;
    var variant1_result = variant1.result; // Verified decoded data with all block names inside
    // #region How to use "internal schemas" for primitevely encoded data types 
    var primitive_octetstring = new org.pkijs.asn1.OCTETSTRING({ value_hex: encoded_sequence }); // Create a primitively encoded OCTETSTRING where internal data is an encoded SEQUENCE

    var asn1_schema_internal = new org.pkijs.asn1.OCTETSTRING({
        name: "outer_block",
        primitive_schema: new org.pkijs.asn1.SEQUENCE({
            name: "block1",
            value: [
                    new org.pkijs.asn1.NULL({
                        name: "block2"
                    })
            ]
        })
    });

    var variant6 = org.pkijs.compareSchema(primitive_octetstring, primitive_octetstring, asn1_schema_internal);
    var variant6_verified = variant4.verified;
    var variant6_block1_tag_num = variant6.result.block1.id_block.tag_number;
    var variant6_block2_tag_num = variant6.result.block2.id_block.tag_number;
    // #endregion 

更多的例子可以在“示例”目录或内部找到PKIjs库。

下载地址https://github.com/GlobalSign/ASN1.js/archive/master.zip

上一篇:JSON网络令牌纯JavaScript实现的
下一篇:如何安装WordPress:应用程序

相关内容

热门推荐