💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 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](http://luca.ntop.org/Teaching/Appunti/asn1.html) ## Index * [type SyntaxError](#SyntaxError) * [func (e SyntaxError) Error() string](#SyntaxError.Error) * [type StructuralError](#StructuralError) * [func (e StructuralError) Error() string](#StructuralError.Error) * [type RawContent](#RawContent) * [type RawValue](#RawValue) * [type Flag](#Flag) * [type Enumerated](#Enumerated) * [type BitString](#BitString) * [func (b BitString) At(i int) int](#BitString.At) * [func (b BitString) RightAlign() []byte](#BitString.RightAlign) * [type ObjectIdentifier](#ObjectIdentifier) * [func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool](#ObjectIdentifier.Equal) * [func (oi ObjectIdentifier) String() string](#ObjectIdentifier.String) * [func Marshal(val interface{}) ([]byte, error)](#Marshal) * [func Unmarshal(b []byte, val interface{}) (rest []byte, err error)](#Unmarshal) * [func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error)](#UnmarshalWithParams) ## type [SyntaxError](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L39 "View Source") ``` type SyntaxError struct { Msg string } ``` SyntaxErrorLeixing表示ASN.1数据不合法。 ### func (SyntaxError) [Error](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L43 "View Source") ``` func (e SyntaxError) Error() string ``` ## type [StructuralError](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L32 "View Source") ``` type StructuralError struct { Msg string } ``` StructuralError表示ASN.1数据合法但接收的Go类型不匹配。 ### func (StructuralError) [Error](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L36 "View Source") ``` func (e StructuralError) Error() string ``` ## type [RawContent](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L382 "View Source") ``` type RawContent []byte ``` RawContent用于标记未解码的应被结构体保留的DER数据。如要使用它,结构体的第一个字段必须是本类型,其它字段不能是本类型。 ## type [RawValue](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L372 "View Source") ``` type RawValue struct { Class, Tag int IsCompound bool Bytes []byte FullBytes []byte // 包括标签和长度 } ``` RawValue代表一个未解码的ASN.1对象。 ## type [Flag](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L263 "View Source") ``` type Flag bool ``` Flag接收任何数据,如果数据存在就设自身为真。 ## type [Enumerated](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L258 "View Source") ``` type Enumerated int ``` Enumerated表示一个明文整数。 ## type [BitString](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L130 "View Source") ``` type BitString struct { Bytes []byte // 字位流打包在字节流里 BitLength int // 字位流的长度 } ``` BitString类型是用于表示ASN.1 BIT STRING类型的结构体。字位流补齐到最近的字节数保存在内存里并记录合法字位数,补齐的位可以为0个。 ### func (BitString) [At](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L137 "View Source") ``` func (b BitString) At(i int) int ``` At方法发挥index位置的字位,如果index出界则返回0。 ### func (BitString) [RightAlign](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L148 "View Source") ``` func (b BitString) RightAlign() []byte ``` RightAlign方法返回b表示的字位流的右对齐版本(即补位在开始部分)切片,该切片可能和b共享底层内存。 ## type [ObjectIdentifier](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L185 "View Source") ``` type ObjectIdentifier []int ``` ObjectIdentifier类型用于表示ASN.1 OBJECT IDENTIFIER类型。 ### func (ObjectIdentifier) [Equal](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L188 "View Source") ``` func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool ``` 如果oi和other代表同一个标识符,Equal方法返回真。 ### func (ObjectIdentifier) [String](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L201 "View Source") ``` func (oi ObjectIdentifier) String() string ``` ## func [Marshal](https://github.com/golang/go/blob/master/src/encoding/asn1/marshal.go#L622 "View Source") ``` 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](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L895 "View Source") ``` 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](https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L901 "View Source") ``` func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) ``` UnmarshalWithParams允许指定val顶层成员的字段参数,格式和字段标签相同。