meowlib/symcrypt.go

69 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-09-18 21:17:28 +02:00
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
}
2022-12-19 21:54:08 +01:00
/*
func SymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) (*EncryptedMessage, error) {
var enc EncryptedMessage
2022-09-18 21:17:28 +02:00
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")
}
2022-12-19 21:54:08 +01:00
enc.Data = []byte(encrypted)
enc.Signature = []byte(signature)
return &enc, err
2022-09-18 21:17:28 +02:00
}
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
}
2022-12-19 21:54:08 +01:00
*/