package meowlib import ( "encoding/base64" "time" "github.com/ProtonMail/gopenpgp/v2/crypto" "github.com/ProtonMail/gopenpgp/v2/helper" "github.com/rs/zerolog/log" ) type KeyPair struct { Public string `json:"public,omitempty"` Private string `json:"private,omitempty"` Generated time.Time `json:"generated,omitempty"` } type KeysArray []KeyPair func NewKeyPair() KeyPair { var kp KeyPair keys, err := crypto.GenerateKey("name", "mail", "rsa", 4096) if err != nil { log.Error().Msg("Key generation failed") } kp.Generated = time.Now() pub, err := keys.GetArmoredPublicKey() if err != nil { log.Error().Msg("Public key extraction failed") } kp.Public = base64.StdEncoding.EncodeToString([]byte(pub)) priv, err := keys.Armor() if err != nil { log.Error().Msg("Private key extraction failed") } kp.Private = base64.StdEncoding.EncodeToString([]byte(priv)) return kp } func (keyPair *KeyPair) GetCryptoKeyObject() *crypto.Key { priv, err := base64.StdEncoding.DecodeString(keyPair.Private) if err != nil { log.Error().Msg("Create key from armoured b64 failed") } key, err := crypto.NewKeyFromArmored(string(priv)) if err != nil { log.Error().Msg("Create key from armoured failed") } return key } func Encrypt(publicKey string, data []byte) ([]byte, error) { pub, err := base64.StdEncoding.DecodeString(publicKey) if err != nil { log.Error().Msg("Message encryption b64 failed") } armor, err := helper.EncryptBinaryMessageArmored(string(pub), data) if err != nil { log.Error().Msg("Message encryption failed") } return []byte(armor), err } func Decrypt(privateKey string, data []byte) ([]byte, error) { priv, err := base64.StdEncoding.DecodeString(privateKey) if err != nil { log.Error().Msg("Message decryption b64 failed") } decrypted, err := helper.DecryptBinaryMessageArmored(string(priv), []byte(""), string(data)) if err != nil { log.Error().Msg("Message decryption failed") } return []byte(decrypted), err } func EncryptAndSign(publicKey string, privateKey string, data []byte) ([]byte, []byte, error) { pub, err := base64.StdEncoding.DecodeString(publicKey) if err != nil { log.Error().Msg("Message encryption and sign b64 failed") } priv, err := base64.StdEncoding.DecodeString(privateKey) if err != nil { log.Error().Msg("Message encryption and sign b64 failed") } armor, signature, err := helper.EncryptSignBinaryDetached(string(pub), string(priv), []byte(""), data) if err != nil { log.Error().Msg("Message encryption and sign failed") } return []byte(armor), []byte(signature), err } func DecryptAndSign(publicKey string, privateKey string, data []byte, signature []byte) ([]byte, error) { pub, err := base64.StdEncoding.DecodeString(publicKey) if err != nil { log.Error().Msg("Message decryption and sign b64 failed") } priv, err := base64.StdEncoding.DecodeString(privateKey) if err != nil { log.Error().Msg("Message decryption and sign b64 failed") } decrypted, err := helper.DecryptVerifyBinaryDetached(string(pub), string(priv), []byte(""), data, string(signature)) if err != nil { log.Error().Msg("Message decryption and sign failed") } return decrypted, err }