2023-01-27 12:07:53 +00:00
|
|
|
package crypt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
_ "embed"
|
2023-01-28 23:59:58 +00:00
|
|
|
"io"
|
2023-04-07 14:35:36 +00:00
|
|
|
"os"
|
2023-01-27 12:07:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/openpgp"
|
|
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed key.gpg
|
|
|
|
var key_gpg []byte
|
2023-01-29 15:08:41 +00:00
|
|
|
var recipients []*openpgp.Entity
|
2023-01-27 12:07:53 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
block, err := armor.Decode(bytes.NewBuffer(key_gpg))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-01-29 15:08:41 +00:00
|
|
|
recip, err := openpgp.ReadEntity(packet.NewReader(block.Body))
|
2023-01-27 12:07:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-01-29 15:08:41 +00:00
|
|
|
recipients = append(recipients, recip)
|
2023-01-27 12:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Enc(name string, data []byte) ([]byte, error) {
|
|
|
|
w := bytes.NewBuffer(nil)
|
2023-01-29 15:08:41 +00:00
|
|
|
wc, err := openpgp.Encrypt(w, recipients, nil, &openpgp.FileHints{
|
2023-01-27 12:07:53 +00:00
|
|
|
IsBinary: true, FileName: name, ModTime: time.Now(),
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err = wc.Write(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wc.Close()
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
2023-01-28 23:59:58 +00:00
|
|
|
|
2023-04-07 14:35:36 +00:00
|
|
|
func Encer(filename string) (io.WriteCloser, error) {
|
|
|
|
w, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-29 15:08:41 +00:00
|
|
|
wc, err := openpgp.Encrypt(w, recipients, nil, &openpgp.FileHints{
|
2023-04-07 14:35:36 +00:00
|
|
|
IsBinary: true, FileName: filename, ModTime: time.Now(),
|
2023-01-28 23:59:58 +00:00
|
|
|
}, nil)
|
|
|
|
return wc, err
|
|
|
|
}
|