1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<h1> Package pem </h1> <ul id="short-nav">
<li><code>import "encoding/pem"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul> <h2 id="pkg-overview">Overview </h2> <p>Package pem implements the PEM data encoding, which originated in Privacy Enhanced Mail. The most common use of PEM encoding today is in TLS keys and certificates. See RFC 1421. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
<li><a href="#Encode">func Encode(out io.Writer, b *Block) error</a></li>
<li><a href="#EncodeToMemory">func EncodeToMemory(b *Block) []byte</a></li>
<li><a href="#Block">type Block</a></li>
<li> <a href="#Decode">func Decode(data []byte) (p *Block, rest []byte)</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_Decode">Decode</a></dd> <dd><a class="exampleLink" href="#example_Encode">Encode</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>pem.go</span> </p> <h2 id="Encode">func <span>Encode</span> </h2> <pre data-language="go">func Encode(out io.Writer, b *Block) error</pre> <p>Encode writes the PEM encoding of b to out. </p> <h4 id="example_Encode"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">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)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">-----BEGIN MESSAGE-----
Animal: Gopher
dGVzdA==
-----END MESSAGE-----
</pre> <h2 id="EncodeToMemory">func <span>EncodeToMemory</span> </h2> <pre data-language="go">func EncodeToMemory(b *Block) []byte</pre> <p>EncodeToMemory returns the PEM encoding of b. </p>
<p>If b has invalid headers and cannot be encoded, EncodeToMemory returns nil. If it is important to report details about this error case, use <a href="#Encode">Encode</a> instead. </p>
<h2 id="Block">type <span>Block</span> </h2> <p>A Block represents a PEM encoded structure. </p>
<p>The encoded form is: </p>
<pre data-language="go">-----BEGIN Type-----
Headers
base64-encoded Bytes
-----END Type-----
</pre> <p>where [Block.Headers] is a possibly empty sequence of Key: Value lines. </p>
<pre data-language="go">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.
}
</pre> <h3 id="Decode">func <span>Decode</span> </h3> <pre data-language="go">func Decode(data []byte) (p *Block, rest []byte)</pre> <p>Decode will find the next PEM formatted block (certificate, private key etc) in the input. It returns that block and the remainder of the input. If no PEM data is found, p is nil and the whole of the input is returned in rest. </p> <h4 id="example_Decode"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">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)
</pre> <p>Output:</p> <pre class="output" data-language="go">Got a *rsa.PublicKey, with remaining data: "and some more"
</pre><div class="_attribution">
<p class="_attribution-p">
© Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
<a href="http://golang.org/pkg/encoding/pem/" class="_attribution-link">http://golang.org/pkg/encoding/pem/</a>
</p>
</div>
|