meowlib/symcrypt.go
2022-12-19 21:54:08 +01:00

69 lines
2.2 KiB
Go

package meowlib
import (
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/pkg/errors"
)
func SymEncrypt(password string, data []byte) ([]byte, error) {
var pgpMessage *crypto.PGPMessage
var err error
var message = crypto.NewPlainMessage(data)
pgpMessage, err = crypto.EncryptMessageWithPassword(message, []byte(password))
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to encrypt message with password")
}
return pgpMessage.GetBinary(), nil
}
func SymDecrypt(password string, data []byte) ([]byte, error) {
var message *crypto.PlainMessage
var pgpMessage *crypto.PGPMessage
var err error
pgpMessage = crypto.NewPGPMessage(data)
message, err = crypto.DecryptMessageWithPassword(pgpMessage, []byte(password))
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message with password")
}
return message.GetBinary(), nil
}
/*
func SymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) (*EncryptedMessage, error) {
var enc EncryptedMessage
pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey)
if err != nil {
log.Error().Msg("Message encryption and sign b64 failed")
}
priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey)
if err != nil {
log.Error().Msg("Message encryption and sign b64 failed")
}
encrypted, signature, err := helper.EncryptSignBinaryDetached(string(pub), string(priv), nil, data)
if err != nil {
log.Error().Msg("Message encryption and sign failed")
}
enc.Data = []byte(encrypted)
enc.Signature = []byte(signature)
return &enc, err
}
func SymDecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) {
pub, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey)
if err != nil {
log.Error().Msg("Message decryption and sign b64 failed")
}
priv, err := base64.StdEncoding.DecodeString(MyContactPublicKey)
if err != nil {
log.Error().Msg("Message decryption and sign b64 failed")
}
DecryptedMessage, err = helper.DecryptVerifyBinaryDetached(string(pub), string(priv), nil, data, string(Signature))
if err != nil {
log.Error().Msg("Message decryption and sign failed")
}
return DecryptedMessage, err
}
*/