npm 包 asn1.js-rfc5280-static 使用教程

阅读时长 20 分钟读完

在前端开发中,我们经常需要解析和生成 ASN.1 编码的数据。asn1.js-rfc5280-static 是一个能够解析和生成 ASN.1 编码数据的 npm 包,本文将介绍它的使用方法。

安装

使用 npm 安装 asn1.js-rfc5280-static:

引入

在需要使用的 js 文件中,引入 asn1.js-rfc5280-static:

使用

1. 生成 ASN.1 编码

生成 ASN.1 编码有两种方法,一种是使用已经定义好的 ASN.1 Schema,另一种是通过自定义的方式生成。

1.1 使用已定义好的 ASN.1 Schema

-- -------------------- ---- -------
----- ------ - -------------------------- ---------- -
  ---------------
    -------------------------------------
      ---------------------------------------------
      ---------------------------------------------
      ------------------------------------------------------
      -----------------------------
      -----------------------------------
      ------------------------------
      ----------------------------------------------------
      ------------------------------------------------------------------------
      -------------------------------------------------------------------------
      -------------------------------------------------------------
    --
    ---------------------------------------------------------------
    ------------------------------------------------
  --
---

----- ----------- - -
  --------------- -
    -------- --
    ------------- --
    ---------- -
      ---------- --------------------------
      ----------- ----
    --
    ------- -
      ------------ -----
      -------------------- -------------
      ----------------- ----- ------
      ----------- ----------
    --
    --------- -
      ---------- --- ---------- -- ---
      --------- --- ---------- --- ---
    --
    -------- -
      ------------ -----
      -------------------- ----------
      ----------------- ----- ---------
      ----------- ---------
    --
    --------------------- -
      ---------- -
        ---------- ----------------
        ----------- ----
      --
      ----------------- --- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------
    -
  --
  ------------------- -
    ---------- --------------------------
    ----------- ----
  -
--

----- ------ - -------------------------- -------
------------------------------------

1.2 通过自定义的方式生成 ASN.1 编码

-- -------------------- ---- -------
----- -------------------- - ----------------------------- ---------- -
  ---------------
    ----------------------------------------
    ---------------------------------------------
    ------------------------------------------------------
    -----------------------------
    -----------------------------------
    ------------------------------
    ----------------------------------------------------
    ------------------------------------------------------------------------
    -------------------------------------------------------------------------
    -------------------------------------------------------------
  --
---

----- ------------- - ---------------------- ---------- -
  -------------
    --------- --
    -- ------- -
    ------ -------------------------
    -- ------- -
    ------ ------------------------------
  ---
---

----- ----------- - -
  --------------- -
    -------- -
      --------- --
      ------ -
    --
    ------------- --
    ---------- -
      ---------- --------------------------
      ----------- ----
    --
    ------- -
      ------------ -----
      -------------------- -------------
      ----------------- ----- ------
      ----------- ----------
    --
    --------- -
      ---------- --- ---------- -- ---
      --------- --- ---------- --- ---
    --
    -------- -
      ------------ -----
      -------------------- ----------
      ----------------- ----- ---------
      ----------- ---------
    --
    --------------------- -
      ---------- -
        ---------- ----------------
        ----------- ----
      --
      ----------------- --- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------
    -
  --
  ------------------- -
    ---------- --------------------------
    ----------- ----
  -
--

----- ------ - -------------------------------------------------------- -------
------------------------------------

2. 解析 ASN.1 编码

解析 ASN.1 编码也有两种方法:

2.1 使用已定义好的 ASN.1 Schema

-- -------------------- ---- -------
----- ------ - -------------------------- ---------- -
  ---------------
    -------------------------------------
      ---------------------------------------------
      ---------------------------------------------
      ------------------------------------------------------
      -----------------------------
      -----------------------------------
      ------------------------------
      ----------------------------------------------------
      ------------------------------------------------------------------------
      -------------------------------------------------------------------------
      -------------------------------------------------------------
    --
    ---------------------------------------------------------------
    ------------------------------------------------
  --
---

----- ----- - -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----- ---- - --- ------------- -------
----- --- - ------------------- -------
------------------------------- ----- ----

2.2 通过自定义的方式解析 ASN.1 编码

-- -------------------- ---- -------
----- ------ - -------------------------- ---------- -
  ---------------
    -------------------------------------
      ----------------------------
        --------- --
        -- ------- -
        ------ -------------------------
        -- ------- -
        ------ -----------------------------------
      ---
      ---------------------------------------------
      ------------------------------------------------------
      -----------------------------
      -----------------------------------
      ------------------------------
      ----------------------------------------------------
      ------------------------------------------------------------------------
      -------------------------------------------------------------------------
      -------------------------------------------------------------
    --
    ---------------------------------------------------------------
    ------------------------------------------------
  --
---

----- -------------------- - ----------------------------- ---------- -
  ---------------
    ---------------------------------
    ---------------------------------------------
    ------------------------------------------------------
    -----------------------------
    -----------------------------------
    ------------------------------
    ----------------------------------------------------
    ------------------------------------------------------------------------
    -------------------------------------------------------------------------
    -------------------------------------------------------------
  --
---

----- ------------- - ---------------------- ---------- -
  -------------
    --------- --
    -- ------- -
    ------ -------------------------
    -- ------- -
    ------ ------------------------------
  ---
---

----- ----- - -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----- ---- - --- ------------- -------
----- --- - ---------------------------------- --- -------
------------------------------- ----- ----

示例代码

完整的示例代码如下:

-- -------------------- ---- -------
----- ---- - ----------------------------------
----- ------ - ------------

----- ---- - ------------------- ---------- -
  ---------------
    ----------------------------------------------------
    -----------------------------------------------------------------------
    ----------------------------------------------------------------
    ---------------------------------------------------------
    --------------------------------------------------------------------------
    ---------------------------------------------------
    ---------------------------------------------------------
  --
---

----- -------- - ----------------------- ---------- -
  ---------------
    ---------------------------------------
    -------------------------------------
  --
---

----- --------- - ------------------------ ---------- -
  ---------------
    ------------------------------------------------------
    --------------------------------------------------
  --
---

----- ------------- - ---------------------------- ---------- -
  ---------------
    ------------------------------------------------------
    -------------------------------------------
  --
---

----- --------- - ------------------------ ---------- -
  ---------------
    ------------------------------------------------
    -----------------------------------------
    ---------------------------------------------
  --
---

----- ---------- - ------------------------- ---------- -
  ----------------------
---

----- ------- - ---------------------- ---------- -
  -------------
    --------- --
    -- ------- -
    ------ -------------------------
    -- ------- -
    ------ -----------------------------------
  ---
---

----- -------------------- - ----------------------------- ---------- -
  ---------------
    ---------------------------------
    ---------------------------------------------
    ------------------------------------------------------
    -----------------------------
    -----------------------------------
    ------------------------------
    --------------------------------------------------

- ------------------------------------------------------------------------------ --------
------------------------------------------------------------------------------------------------------------------------
纠错
反馈