Movatterモバイル変換


[0]ホーム

URL:


pem

packagestandard library
go1.25.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:6Imported by:58,182

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package pem implements the PEM data encoding, which originated in PrivacyEnhanced Mail. The most common use of PEM encoding today is in TLS keys andcertificates. SeeRFC 1421.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcEncode

func Encode(outio.Writer, b *Block)error

Encode writes the PEM encoding of b to out.

Example
package mainimport ("encoding/pem""log""os")func main() {block := &pem.Block{Type: "MESSAGE",Headers: map[string]string{"Animal": "Gopher",},Bytes: []byte("test"),}if err := pem.Encode(os.Stdout, block); err != nil {log.Fatal(err)}}
Output:-----BEGIN MESSAGE-----Animal: GopherdGVzdA==-----END MESSAGE-----

funcEncodeToMemory

func EncodeToMemory(b *Block) []byte

EncodeToMemory returns the PEM encoding of b.

If b has invalid headers and cannot be encoded,EncodeToMemory returns nil. If it is important toreport details about this error case, useEncode instead.

Types

typeBlock

type Block struct {Typestring// 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.}

A Block represents a PEM encoded structure.

The encoded form is:

-----BEGIN Type-----Headersbase64-encoded Bytes-----END Type-----

where [Block.Headers] is a possibly empty sequence of Key: Value lines.

funcDecode

func Decode(data []byte) (p *Block, rest []byte)

Decode will find the next PEM formatted block (certificate, private keyetc) in the input. It returns that block and the remainder of the input. Ifno PEM data is found, p is nil and the whole of the input is returned inrest. Blocks must start at the beginning of a line and end at the end of a line.

Example
package mainimport ("crypto/x509""encoding/pem""fmt""log")func main() {var pubPEMData = []byte(`-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAlRuRnThUjU8/prwYxbtyWPT9pURI3lbsKMiB6Fn/VHOKE13p4D8xgOCADpdRagdT6n4etr9atzDKUSvpMtR3CP5noNc97WiNCggBjVWhs7szEe8ugyqF23XwpHQ6uV1LKH50m92MbOWfCtjU9p/xqhNpQQ1AZhqNy5Gevap5k8XzRmjSldNAFZMY7Yv3Gi+nyCwGwpVtBUwhuLzgNFK/yDtw2WcWmUU7NuC8Q6MWvPebxVtCfVp/iQU6q60yyt6aGOBkhAX0LpKAEhKidixYnP9PNVBvxgu3XZ4P36gZV6+ummKdBVnc3NqwBLu5+CcdRdusmHPHd5pHf4/38Z3/6qU2a/fPvWzceVTEgZ47QjFMTCTmCwNt29cvi7zZeQzjtwQgn4ipN9NibRH/Ax/qTbIzHfrJ1xa2RteWSdFjwtxi9C20HUkjXSeI4YlzQMH0fPX6KCE7aVePTOnB69I/a9/q96DiXZajwlpq3wFctrs1oXqBp5DVrCIj8hU2wNgB7LtQ1mCtsYz//heai0K9PhE4X6hiE0YmeAZjR0uHl8M/5aW9xCoJ72+12kKpWAa0SFRWLy6FejNYCYpkupVJyecLk/4L1W0l6jQQZnWErXZYe0PNFcmwGXy1Rep83kfBRNKRy5tvocalLlwXLdUkAIU+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)}
Output:Got a *rsa.PublicKey, with remaining data: "and some more"

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp