AsymCrypt optimize + Symcrypt creation
This commit is contained in:
67
symcrypt.go
Normal file
67
symcrypt.go
Normal file
@ -0,0 +1,67 @@
|
||||
package meowlib
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
"github.com/ProtonMail/gopenpgp/v2/helper"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
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) ([]byte, []byte, error) {
|
||||
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")
|
||||
}
|
||||
return []byte(encrypted), []byte(signature), 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
|
||||
}
|
Reference in New Issue
Block a user