1// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2// this source code is governed by the included BSD license.
3
4package libkb
5
6import (
7	"io"
8	"strings"
9
10	"github.com/keybase/go-crypto/openpgp"
11)
12
13func PGPEncrypt(source io.Reader, sink io.WriteCloser, signer *PGPKeyBundle, recipients []*PGPKeyBundle) error {
14	to := make([]*openpgp.Entity, len(recipients))
15	for i, r := range recipients {
16		to[i] = r.Entity
17	}
18	var signerEntity *openpgp.Entity
19	if signer != nil {
20		signerEntity = signer.Entity
21	}
22	w, err := openpgp.Encrypt(sink, to, signerEntity, &openpgp.FileHints{IsBinary: true}, nil)
23	if err != nil {
24		return err
25	}
26	_, err = io.Copy(w, source)
27	if err != nil {
28		return err
29	}
30	if err := w.Close(); err != nil {
31		return err
32	}
33	return sink.Close()
34}
35
36func PGPEncryptString(input string, signer *PGPKeyBundle, recipients []*PGPKeyBundle) ([]byte, error) {
37	source := strings.NewReader(input)
38	sink := NewBufferCloser()
39	if err := PGPEncrypt(source, sink, signer, recipients); err != nil {
40		return nil, err
41	}
42	return sink.Bytes(), nil
43}
44