# 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頂層成員的字段參數,格式和字段標簽相同。
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe