-
Notifications
You must be signed in to change notification settings - Fork 15
/
example_test.go
126 lines (102 loc) · 3.23 KB
/
example_test.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package pgpmail_test
import (
"bytes"
"io"
"log"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/emersion/go-message"
"github.com/emersion/go-message/mail"
"github.com/emersion/go-pgpmail"
)
func ExampleRead() {
// Let's assume r contains an e-mail, which is maybe encrypted or signed
var r io.Reader
// A private key is needed in case the message is encrypted
var privateKey *openpgp.Entity
pgpReader, err := pgpmail.Read(r, openpgp.EntityList{privateKey}, nil, nil)
if err != nil {
log.Fatal(err)
}
log.Printf("Header: %v", pgpReader.Header)
// pgpReader.MessageDetails.UnverifiedBody contains the whole wrapped e-mail
entity, err := message.Read(pgpReader.MessageDetails.UnverifiedBody)
if err != nil {
log.Fatal(err)
}
// Do something with the wrapped e-mail
log.Printf("Wrapped header: %v", entity.Header)
var buf bytes.Buffer
if _, err := io.Copy(&buf, entity.Body); err != nil {
log.Fatal(err)
}
// Now that the wrapped e-mail has been read, we can check the signature.
// We can only do this if the wrapped e-mail has been fully consumed.
if err := pgpReader.MessageDetails.SignatureError; err != nil {
log.Fatal(err)
}
log.Printf("Signed: %v", pgpReader.MessageDetails.IsSigned)
log.Printf("Encrypted: %v", pgpReader.MessageDetails.IsEncrypted)
}
func ExampleEncrypt() {
// to are the recipients' keys, signer is the sender's key
var to []*openpgp.Entity
var signer *openpgp.Entity
var mailHeader mail.Header
mailHeader.SetAddressList("From", []*mail.Address{{"Mitsuha Miyamizu", "mitsuha.miyamizu@example.org"}})
mailHeader.SetAddressList("To", []*mail.Address{{"Taki Tachibana", "taki.tachibana@example.org"}})
var encryptedHeader mail.Header
encryptedHeader.SetContentType("text/plain", nil)
encryptedText := "Hi! I'm Mitsuha Miyamizu."
var buf bytes.Buffer
cleartext, err := pgpmail.Encrypt(&buf, mailHeader.Header.Header, to, signer, nil)
if err != nil {
log.Fatal(err)
}
defer cleartext.Close()
body, err := mail.CreateSingleInlineWriter(cleartext, encryptedHeader)
if err != nil {
log.Fatal(err)
}
defer body.Close()
if _, err := io.WriteString(body, encryptedText); err != nil {
log.Fatal(err)
}
if err := body.Close(); err != nil {
log.Fatal(err)
}
if err := cleartext.Close(); err != nil {
log.Fatal(err)
}
log.Print(buf.String())
}
func ExampleSign() {
// signer is the sender's key
var signer *openpgp.Entity
var mailHeader mail.Header
mailHeader.SetAddressList("From", []*mail.Address{{"Mitsuha Miyamizu", "mitsuha.miyamizu@example.org"}})
mailHeader.SetAddressList("To", []*mail.Address{{"Taki Tachibana", "taki.tachibana@example.org"}})
var signedHeader mail.Header
signedHeader.SetContentType("text/plain", nil)
signedText := "Hi! I'm Mitsuha Miyamizu."
var buf bytes.Buffer
cleartext, err := pgpmail.Sign(&buf, mailHeader.Header.Header, signer, nil)
if err != nil {
log.Fatal(err)
}
defer cleartext.Close()
body, err := mail.CreateSingleInlineWriter(cleartext, signedHeader)
if err != nil {
log.Fatal(err)
}
defer body.Close()
if _, err := io.WriteString(body, signedText); err != nil {
log.Fatal(err)
}
if err := body.Close(); err != nil {
log.Fatal(err)
}
if err := cleartext.Close(); err != nil {
log.Fatal(err)
}
log.Print(buf.String())
}