Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypt verify message armored #246

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,55 @@ func DecryptMessageArmored(
// plain data or an error on signature verification failure.
func DecryptVerifyMessageArmored(
publicKey, privateKey string, passphrase []byte, ciphertext string,
) (plaintext string, err error) {
) (plainMessage string, err error) {
message, err := decryptVerifyDataArmored(publicKey, privateKey, passphrase, ciphertext)
return message.GetString(), err
}

// DecryptVerifyBinaryMessageArmored decrypts an armored PGP binary given a private
// key and its passphrase and verifies the embedded signature. Returns the
// binary data or an error on signature verification failure.
func DecryptVerifyBinaryMessageArmored(
publicKey, privateKey string, passphrase []byte, data []byte,
) (plainData []byte, err error) {
message, err := decryptVerifyDataArmored(publicKey, privateKey, passphrase, string(data))
return message.GetBinary(), err
}

func decryptVerifyDataArmored(
publicKey, privateKey string, passphrase []byte, ciphertext string,
) (massage *crypto.PlainMessage, err error) {
var privateKeyObj, unlockedKeyObj *crypto.Key
var publicKeyRing, privateKeyRing *crypto.KeyRing
var pgpMessage *crypto.PGPMessage
var message *crypto.PlainMessage

if publicKeyRing, err = createPublicKeyRing(publicKey); err != nil {
return "", err
return message, err
}

if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil {
return "", errors.Wrap(err, "gopenpgp: unable to unarmor private key")
return message, errors.Wrap(err, "gopenpgp: unable to unarmor private key")
}

if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil {
return "", errors.Wrap(err, "gopenpgp: unable to unlock private key")
return message, errors.Wrap(err, "gopenpgp: unable to unlock private key")
}
defer unlockedKeyObj.ClearPrivateParams()

if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil {
return "", errors.Wrap(err, "gopenpgp: unable to create new keyring")
return message, errors.Wrap(err, "gopenpgp: unable to create new keyring")
}

if pgpMessage, err = crypto.NewPGPMessageFromArmored(ciphertext); err != nil {
return "", errors.Wrap(err, "gopenpgp: unable to unarmor ciphertext")
return message, errors.Wrap(err, "gopenpgp: unable to unarmor ciphertext")
}

if message, err = privateKeyRing.Decrypt(pgpMessage, publicKeyRing, crypto.GetUnixTime()); err != nil {
return "", errors.Wrap(err, "gopenpgp: unable to decrypt message")
return message, errors.Wrap(err, "gopenpgp: unable to decrypt message")
}

return message.GetString(), nil
return message, nil
}

// DecryptVerifyAttachment decrypts and verifies an attachment split into the
Expand Down