-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pgp_enc.go
43 lines (38 loc) · 1.03 KB
/
pgp_enc.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"io"
"strings"
"github.com/keybase/go-crypto/openpgp"
)
func PGPEncrypt(source io.Reader, sink io.WriteCloser, signer *PGPKeyBundle, recipients []*PGPKeyBundle) error {
to := make([]*openpgp.Entity, len(recipients))
for i, r := range recipients {
to[i] = r.Entity
}
var signerEntity *openpgp.Entity
if signer != nil {
signerEntity = signer.Entity
}
w, err := openpgp.Encrypt(sink, to, signerEntity, &openpgp.FileHints{IsBinary: true}, nil)
if err != nil {
return err
}
_, err = io.Copy(w, source)
if err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
return sink.Close()
}
func PGPEncryptString(input string, signer *PGPKeyBundle, recipients []*PGPKeyBundle) ([]byte, error) {
source := strings.NewReader(input)
sink := NewBufferCloser()
if err := PGPEncrypt(source, sink, signer, recipients); err != nil {
return nil, err
}
return sink.Bytes(), nil
}