-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencrypt.go
62 lines (51 loc) · 1.39 KB
/
encrypt.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
package envelope
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
)
type EncryptOpts struct {
Encoder func([]byte, io.Writer) error
WithPrefix bool
}
func NopEncoder(in []byte, buf io.Writer) error {
_, err := io.Copy(buf, bytes.NewReader(in))
return err
}
func Base64Encoder(in []byte, buf io.Writer) error {
encoder := base64.NewEncoder(base64.StdEncoding, buf)
_, err := encoder.Write(in)
encoder.Close()
return err
}
// EncryptWithOpts will encypt the input as a blob using the key given and the options specified
func (s *Envelope) EncryptWithOpts(alias string, input io.Reader, opts EncryptOpts) ([]byte, error) {
key, ok := s.Keyring.GetKey(alias)
if !ok {
return []byte(""), fmt.Errorf("Couldn't find key with alias or id '%s'", alias)
}
data, err := ioutil.ReadAll(input)
if err != nil {
return []byte(""), err
}
encrypted, err := key.EncryptBytes(data)
if err != nil {
return []byte(""), err
}
var output []byte
buffer := bytes.NewBuffer(output)
if opts.WithPrefix {
buffer.Write([]byte(s.Prefix))
}
err = opts.Encoder(encrypted, buffer)
if err != nil {
return []byte(""), err
}
return ioutil.ReadAll(buffer)
}
// Encrypt will encrypt the input as a blob using the key given and default options
func (s *Envelope) Encrypt(alias string, input io.Reader) ([]byte, error) {
return s.EncryptWithOpts(alias, input, EncryptOpts{Encoder: NopEncoder})
}