AsymCrypt optimize + Symcrypt creation
This commit is contained in:
96
asymcrypt.go
96
asymcrypt.go
@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
"github.com/ProtonMail/gopenpgp/v2/helper"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@ -49,7 +50,33 @@ func (Kp *KeyPair) GetCryptoKeyObject() *crypto.Key {
|
||||
return key
|
||||
}
|
||||
|
||||
func Encrypt(PublicKey string, data []byte) ([]byte, error) {
|
||||
func AsymEncrypt(publicKey string, data []byte) ([]byte, error) {
|
||||
pub, err := base64.StdEncoding.DecodeString(publicKey)
|
||||
if err != nil {
|
||||
log.Error().Msg("Message encryption b64 failed")
|
||||
}
|
||||
ciphertext, err := encryptMessage(string(pub), crypto.NewPlainMessage(data))
|
||||
if err != nil {
|
||||
log.Error().Msg("Message encryption failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ciphertext.GetBinary(), err
|
||||
}
|
||||
|
||||
func AsymDecrypt(PrivateKey string, data []byte) ([]byte, error) {
|
||||
priv, err := base64.StdEncoding.DecodeString(PrivateKey)
|
||||
if err != nil {
|
||||
log.Error().Msg("Message decryption b64 failed")
|
||||
}
|
||||
decrypted, err := decryptMessage(string(priv), nil, crypto.NewPGPMessage(data))
|
||||
if err != nil {
|
||||
log.Error().Msg("Message decryption failed")
|
||||
}
|
||||
return decrypted.GetBinary(), err
|
||||
}
|
||||
|
||||
func AsymEncryptArmored(PublicKey string, data []byte) ([]byte, error) {
|
||||
pub, err := base64.StdEncoding.DecodeString(PublicKey)
|
||||
if err != nil {
|
||||
log.Error().Msg("Message encryption b64 failed")
|
||||
@ -61,7 +88,7 @@ func Encrypt(PublicKey string, data []byte) ([]byte, error) {
|
||||
return []byte(armor), err
|
||||
}
|
||||
|
||||
func Decrypt(PrivateKey string, data []byte) ([]byte, error) {
|
||||
func AsymDecryptArmored(PrivateKey string, data []byte) ([]byte, error) {
|
||||
priv, err := base64.StdEncoding.DecodeString(PrivateKey)
|
||||
if err != nil {
|
||||
log.Error().Msg("Message decryption b64 failed")
|
||||
@ -73,7 +100,7 @@ func Decrypt(PrivateKey string, data []byte) ([]byte, error) {
|
||||
return []byte(decrypted), err
|
||||
}
|
||||
|
||||
func EncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) ([]byte, []byte, error) {
|
||||
func AsymEncryptAndSign(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")
|
||||
@ -89,7 +116,7 @@ func EncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data
|
||||
return []byte(encrypted), []byte(signature), err
|
||||
}
|
||||
|
||||
func DecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) {
|
||||
func AsymDecryptAndCheck(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")
|
||||
@ -104,3 +131,64 @@ func DecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, d
|
||||
}
|
||||
return DecryptedMessage, err
|
||||
}
|
||||
|
||||
func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessage, error) {
|
||||
publicKeyRing, err := createPublicKeyRing(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ciphertext, err := publicKeyRing.Encrypt(message, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to encrypt message")
|
||||
}
|
||||
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
func decryptMessage(privateKey string, passphrase []byte, ciphertext *crypto.PGPMessage) (*crypto.PlainMessage, error) {
|
||||
privateKeyObj, err := crypto.NewKeyFromArmored(privateKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to parse the private key")
|
||||
}
|
||||
|
||||
privateKeyUnlocked, err := privateKeyObj.Unlock(passphrase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to unlock key")
|
||||
}
|
||||
|
||||
defer privateKeyUnlocked.ClearPrivateParams()
|
||||
|
||||
privateKeyRing, err := crypto.NewKeyRing(privateKeyUnlocked)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to create the private key ring")
|
||||
}
|
||||
|
||||
message, err := privateKeyRing.Decrypt(ciphertext, nil, 0)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message")
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) {
|
||||
publicKeyObj, err := crypto.NewKeyFromArmored(publicKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to parse public key")
|
||||
}
|
||||
|
||||
if publicKeyObj.IsPrivate() {
|
||||
publicKeyObj, err = publicKeyObj.ToPublic()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to extract public key from private key")
|
||||
}
|
||||
}
|
||||
|
||||
publicKeyRing, err := crypto.NewKeyRing(publicKeyObj)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "gopenpgp: unable to create new keyring")
|
||||
}
|
||||
|
||||
return publicKeyRing, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user