go-pgp/pgp/encypt.go

44 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-07-25 16:25:01 +02:00
package pgp
import (
"bytes"
_ "crypto/sha256"
"fmt"
"io"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
_ "golang.org/x/crypto/ripemd160"
)
func Encrypt(entity *openpgp.Entity, message []byte) ([]byte, error) {
// Create buffer to write output to
buf := new(bytes.Buffer)
// Create encoder
encoderWriter, err := armor.Encode(buf, "PGP MESSAGE", make(map[string]string))
if err != nil {
return []byte{}, fmt.Errorf("error creating OpenPGP armor: %v", err)
}
// Create encryptor with encoder
encryptorWriter, err := openpgp.Encrypt(encoderWriter, []*openpgp.Entity{entity}, nil, nil, nil)
if err != nil {
return []byte{}, fmt.Errorf("Error creating entity for encryption: %v", err)
}
// Write message to compressor
messageReader := bytes.NewReader(message)
_, err = io.Copy(encryptorWriter, messageReader)
if err != nil {
return []byte{}, fmt.Errorf("Error writing data to message reader: %v", err)
}
//compressorWriter.Close()
encryptorWriter.Close()
encoderWriter.Close()
// Return buffer output - an encoded, encrypted, and compressed message
return buf.Bytes(), nil
}