©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
import "encoding/pem"
Overview
Index
Examples
Package pem实现了PEM数据编码,该编码源于Privacy Enhanced Mail。目前最常见的PEM编码用在TLS密钥和证书中。请参阅RFC 1421。
func Encode(out io.Writer, b *Block) error
func EncodeToMemory(b *Block) []byte
type Block
func Decode(data []byte) (p *Block, rest []byte)
解码
pem.go
func Encode(out io.Writer, b *Block) error
func EncodeToMemory(b *Block) []byte
块表示PEM编码结构。
编码形式是:
-----BEGIN Type-----Headers base64-encoded Bytes-----END Type-----
其中标题是一个可能为空的Key:Value行的序列。
type Block struct { Type string // The type, taken from the preamble (i.e. "RSA PRIVATE KEY"). Headers map[string]string // Optional headers. Bytes []byte // The decoded bytes of the contents. Typically a DER encoded ASN.1 structure.}
func Decode(data []byte) (p *Block, rest []byte)
解码将在输入中找到下一个PEM格式化的块(证书,私钥等)。它返回该块和输入的其余部分。如果没有找到PEM数据,则p为零,并且整个输入在休息时返回。
package mainimport ("crypto/x509""encoding/pem""fmt""log")func main() {var pubPEMData = []byte(` -----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAlRuRnThUjU8/prwYxbty WPT9pURI3lbsKMiB6Fn/VHOKE13p4D8xgOCADpdRagdT6n4etr9atzDKUSvpMtR3 CP5noNc97WiNCggBjVWhs7szEe8ugyqF23XwpHQ6uV1LKH50m92MbOWfCtjU9p/x qhNpQQ1AZhqNy5Gevap5k8XzRmjSldNAFZMY7Yv3Gi+nyCwGwpVtBUwhuLzgNFK/ yDtw2WcWmUU7NuC8Q6MWvPebxVtCfVp/iQU6q60yyt6aGOBkhAX0LpKAEhKidixY nP9PNVBvxgu3XZ4P36gZV6+ummKdBVnc3NqwBLu5+CcdRdusmHPHd5pHf4/38Z3/ 6qU2a/fPvWzceVTEgZ47QjFMTCTmCwNt29cvi7zZeQzjtwQgn4ipN9NibRH/Ax/q TbIzHfrJ1xa2RteWSdFjwtxi9C20HUkjXSeI4YlzQMH0fPX6KCE7aVePTOnB69I/ a9/q96DiXZajwlpq3wFctrs1oXqBp5DVrCIj8hU2wNgB7LtQ1mCtsYz//heai0K9 PhE4X6hiE0YmeAZjR0uHl8M/5aW9xCoJ72+12kKpWAa0SFRWLy6FejNYCYpkupVJ yecLk/4L1W0l6jQQZnWErXZYe0PNFcmwGXy1Rep83kfBRNKRy5tvocalLlwXLdUk AIU+2GKjyT3iMuzZxxFxPFMCAwEAAQ== -----END PUBLIC KEY----- and some more`) block, rest := pem.Decode(pubPEMData)if block == nil || block.Type != "PUBLIC KEY" { log.Fatal("failed to decode PEM block containing public key")} pub, err := x509.ParsePKIXPublicKey(block.Bytes)if err != nil { log.Fatal(err)} fmt.Printf("Got a %T, with remaining data: %q", pub, rest)}