pgpmail

package module
v0.0.0-...-3b3035a Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: MIT Imports: 14 Imported by: 0

README

go-pgpmail

GoDoc

A mail library that encrypts messages with PGP.

License

MIT

Documentation

Overview

Package pgpmail implements PGP encryption for e-mail messages.

PGP/MIME is defined in RFC 3156.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encrypt

func Encrypt(w io.Writer, h textproto.Header, to []*openpgp.Entity, signed *openpgp.Entity, config *packet.Config) (io.WriteCloser, error)
Example
// 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(cleartext, 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())
Output:

func Sign

func Sign(w io.Writer, header, signedHeader textproto.Header, signed *openpgp.Entity, config *packet.Config) (io.WriteCloser, error)

Types

type Reader

type Reader struct {
	Header         textproto.Header
	MessageDetails *openpgp.MessageDetails
}

func NewReader

func NewReader(h textproto.Header, body io.Reader, keyring openpgp.KeyRing, prompt openpgp.PromptFunction, config *packet.Config) (*Reader, error)

func Read

func Read(r io.Reader, keyring openpgp.KeyRing, prompt openpgp.PromptFunction, config *packet.Config) (*Reader, error)
Example
// 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)
Output:

Jump to

Keyboard shortcuts

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