pgpmime

package module
v0.0.0-...-e151e6b Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2019 License: MIT Imports: 11 Imported by: 0

README

go-pgpmime

GoDoc Build Status

A PGP/MIME library written in Go.

License

MIT

Documentation

Overview

pgpmime implements MIME security with OpenPGP, as defined in RFC 3156.

Index

Examples

Constants

View Source
const MessageType = "PGP MESSAGE"

MessageType is the armored type for PGP encrypted messages.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer interface {
	io.WriteCloser

	// ContentType returns the content type of the PGP/MIME message.
	ContentType() string
}

Writer writes a PGP/MIME message body.

func Encrypt

func Encrypt(w io.Writer, h textproto.MIMEHeader, to []*openpgp.Entity, signed *openpgp.Entity, config *packet.Config) (cleartext Writer)

Encrypt creates a new encrypted PGP/MIME message writer.

Example
var b bytes.Buffer

// Create the mail header
mh := mail.NewHeader()
mh.SetAddressList("From", []*mail.Address{{"Mitsuha Miyamizu", "mitsuha.miyamizu@example.org"}})
mh.SetSubject("Your Name")

// Create the text part header
th := mail.NewTextHeader()
th.SetContentType("text/plain", nil)

// Create a new PGP/MIME writer
var ciphertext struct{ *message.Writer }
cleartext := pgpmime.Encrypt(&ciphertext, textproto.MIMEHeader(th.Header), to, nil, nil)

// Add the PGP/MIME Content-Type header field to the mail header
mh.Set("Content-Type", cleartext.ContentType())

// Create a new mail writer with our mail header
mw, err := message.CreateWriter(&b, mh.Header)
if err != nil {
	log.Fatal(err)
}
// Set the PGP/MIME writer output to the mail body
ciphertext.Writer = mw

// Write the cleartext body
_, err = io.WriteString(cleartext, "What's your name?")
if err != nil {
	log.Fatal(err)
}

// Close all writers
if err := cleartext.Close(); err != nil {
	log.Fatal(err)
}
if err := mw.Close(); err != nil {
	log.Fatal(err)
}

log.Println(b.String())
Output:

func Sign

func Sign(w io.Writer, h textproto.MIMEHeader, signer *openpgp.Entity, config *packet.Config) (message Writer)

Sign creates a new signed PGP/MIME message writer.

Example
var b bytes.Buffer

e, err := openpgp.NewEntity("Mitsuha Miyamizu", "", "mitsuha.miyamizu@example.org", nil)
if err != nil {
	log.Fatal(err)
}

mh := mail.NewHeader()
mh.SetAddressList("From", []*mail.Address{{"Mitsuha Miyamizu", "mitsuha.miyamizu@example.org"}})
mh.SetSubject("Your Name")

bh := mail.NewTextHeader()
bh.SetContentType("text/plain", nil)

var signed struct{ *message.Writer }
body := pgpmime.Sign(&signed, textproto.MIMEHeader(bh.Header), e, nil)

mh.Set("Content-Type", body.ContentType())

mw, err := message.CreateWriter(&b, mh.Header)
if err != nil {
	log.Fatal(err)
}
signed.Writer = mw

_, err = io.WriteString(body, "What's your name?")
if err != nil {
	log.Fatal(err)
}

if err := body.Close(); err != nil {
	log.Fatal(err)
}
if err := mw.Close(); err != nil {
	log.Fatal(err)
}

log.Println(b.String())
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL