Go 语言

Go 语言教程 Go 语言环境安装 Go 语言结构 Go 语言基础语法 Go 语言数据类型 Go 语言变量 Go 语言常量 Go 语言运算符 Go 语言条件语句 Go 语言 if 语句 Go 语言 if...else 语句 Go 语言 if 语句嵌套 Go 语言 switch 语句 Go 语言 select 语句 Go 语言循环语句 Go 语言 for 循环 Go 语言循环嵌套 Go 语言 break 语句 Go 语言 continue 语句 Go 语言 goto 语句 Go 语言函数 Go 语言函数值传递值 Go 语言函数引用传递值 Go 语言函数作为值 Go 语言函数闭包 Go 语言函数方法 Go 语言变量作用域 Go 语言数组 Go 语言多维数组 Go 语言向函数传递数组 Go 语言指针 Go 语言指针数组 Go 语言指向指针的指针 Go 语言指针作为函数参数 Go 语言结构体 Go 语言切片(Slice) Go 语言范围(Range) Go 语言Map(集合) Go 语言递归函数 Go 语言类型转换 Go 语言接口 Go 错误处理 Go 语言开发工具Go 语言标准库

Go 语言标准库


package asn1

import "encoding/asn1"

asn1包实现了DER编码的ASN.1数据结构的解析,参见ITU-T Rec X.690。

其他细节参见"A Layman's Guide to a Subset of ASN.1, BER, and DER"。

网址http://luca.ntop.org/Teaching/Appunti/asn1.html

Go语言标准库 >>


  • type SyntaxError
  • type StructuralError
  • type RawContent
  • type RawValue
  • type Flag
  • type Enumerated
  • type BitString
  • type ObjectIdentifier
  • func Marshal(val interface{}) ([]byte, error)
  • func Unmarshal(b []byte, val interface{}) (rest []byte, err error)
  • func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error)
  • type SyntaxError

    type SyntaxError struct {
        Msg string
    }

    SyntaxErrorLeixing表示ASN.1数据不合法。

    func (SyntaxError) Error

    func (e SyntaxError) Error() string

    type StructuralError

    type StructuralError struct {
        Msg string
    }

    StructuralError表示ASN.1数据合法但接收的Go类型不匹配。

    func (StructuralError) Error

    func (e StructuralError) Error() string

    type RawContent

    type RawContent []byte

    RawContent用于标记未解码的应被结构体保留的DER数据。如要使用它,结构体的第一个字段必须是本类型,其它字段不能是本类型。

    type RawValue

    type RawValue struct {
        Class, Tag int
        IsCompound bool
        Bytes      []byte
        FullBytes  []byte // 包括标签和长度
    }

    RawValue代表一个未解码的ASN.1对象。

    type Flag

    type Flag bool

    Flag接收任何数据,如果数据存在就设自身为真。

    type Enumerated

    type Enumerated int

    Enumerated表示一个明文整数。

    type BitString

    type BitString struct {
        Bytes     []byte // 字位流打包在字节流里
        BitLength int    // 字位流的长度
    }

    BitString类型是用于表示ASN.1 BIT STRING类型的结构体。字位流补齐到最近的字节数保存在内存里并记录合法字位数,补齐的位可以为0个。

    func (BitString) At

    func (b BitString) At(i int) int

    At方法发挥index位置的字位,如果index出界则返回0。

    func (BitString) RightAlign

    func (b BitString) RightAlign() []byte

    RightAlign方法返回b表示的字位流的右对齐版本(即补位在开始部分)切片,该切片可能和b共享底层内存。

    type ObjectIdentifier

    type ObjectIdentifier []int

    ObjectIdentifier类型用于表示ASN.1 OBJECT IDENTIFIER类型。

    func (ObjectIdentifier) Equal

    func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool

    如果oi和other代表同一个标识符,Equal方法返回真。

    func (ObjectIdentifier) String

    func (oi ObjectIdentifier) String() string

    func Marshal

    func Marshal(val interface{}) ([]byte, error)

    Marshal函数返回val的ASN.1编码。

    此外还提供了供Unmarshal函数识别的结构体标签,可用如下标签:

    ia5:           使字符串序列化为ASN.1 IA5String类型
    omitempty:     使空切片被跳过
    printable:     使字符串序列化为ASN.1 PrintableString类型
    utf8:          使字符串序列化为ASN.1 UTF8字符串

    func Unmarshal

    func Unmarshal(b []byte, val interface{}) (rest []byte, err error)

    Unmarshal函数解析DER编码的ASN.1结构体数据并使用reflect包填写val指向的任意类型值。因为本函数使用了reflect包,结构体必须使用大写字母起始的字段名。

    ASN.1 INTEGER 类型值可以写入int、int32、int64或*big.Int(math/big包)类型。类型不匹配会返回解析错误。

    ASN.1 BIT STRING类型值可以写入BitString类型。

    ASN.1 OCTET STRING类型值可以写入[]byte类型。

    ASN.1 OBJECT IDENTIFIER类型值可以写入ObjectIdentifier类型。

    ASN.1 ENUMERATED类型值可以写入Enumerated类型。

    ASN.1 UTCTIME类型值或GENERALIZEDTIME 类型值可以写入time.Time类型。

    ASN.1 PrintableString类型值或者IA5String类型值可以写入string类型。

    以上任一ASN.1类型值都可写入interface{}类型。保存在接口里的类型为对应的Go类型,ASN.1整型对应int64。

    如果类型x可以写入切片的成员类型,则类型x的ASN.1 SEQUENCE或SET类型可以写入该切片。

    ASN.1 SEQUENCE或SET类型如果其每一个成员都可以写入某结构体的对应字段,则可以写入该结构体

    对Unmarshal函数,下列字段标签有特殊含义:

    application    指明使用了APPLICATION标签
    default:x      设置一个可选整数字段的默认值
    explicit       给一个隐式的标签设置一个额外的显式标签
    optional       标记字段为ASN.1 OPTIONAL的
    set            表示期望一个SET而不是SEQUENCE类型
    tag:x          指定ASN.1标签码,隐含ASN.1 CONTEXT SPECIFIC

    如果结构体的第一个字段的类型为RawContent,则会将原始ASN1结构体内容包存在该字段。

    如果切片成员的类型名以"SET"结尾,则视为该字段有"set"标签。这是给不能使用标签的嵌套切片使用的。

    其它ASN.1类型不支持,如果遭遇这些类型,Unmarshal返回解析错误。

    func UnmarshalWithParams

    func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error)

    UnmarshalWithParams允许指定val顶层成员的字段参数,格式和字段标签相同。