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

Cannot specify filename when encrypting + signing #289

Open
WordsmithCreativity opened this issue Jul 18, 2024 · 0 comments
Open

Cannot specify filename when encrypting + signing #289

WordsmithCreativity opened this issue Jul 18, 2024 · 0 comments

Comments

@WordsmithCreativity
Copy link

In both v2 and v3 of the library there is no possibility to specify a filename when both signing and encrypting a file.

This would mimic the behavior of gpg as such:

$ gpg --encrypt --sign -r XXX test.txt
$ gpg --list-packets test.txt.gpg
...
:literal data packet:
	mode b (62), created 1721297550, name="test.txt",
	raw data: 9 bytes
...
:signature packet: algo 1, keyid XXX
...

I've altered the https://github.com/ProtonMail/gopenpgp/blob/main/helper/sign_detached.go#L14 function slightly to allow my use-case as such:

func SignAndEncryptWithFileName(publicKey string, privateKey string, passphrase []byte, plaintext string, filename string) (string, error) {
	var privateKeyObj, unlockedKeyObj *crypto.Key
	var publicKeyRing, privateKeyRing *crypto.KeyRing
	var pgpMessage *crypto.PGPMessage
	var ciphertext string

	var message = crypto.NewPlainMessageFromFile([]byte(plaintext), filename, uint32(crypto.GetUnixTime()))

	publicKeyObj, err := crypto.NewKeyFromArmored(publicKey)
	if err != nil {
		return "", err
	}

	publicKeyRing, err = crypto.NewKeyRing(publicKeyObj)
	if err != nil {
		return "", err
	}

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

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

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

	if pgpMessage, err = publicKeyRing.Encrypt(message, privateKeyRing); err != nil {
		return "", errors.New("gopenpgp: unable to encrypt message")
	}

	if ciphertext, err = pgpMessage.GetArmored(); err != nil {
		return "", errors.New("gopenpgp: unable to armor ciphertext")
	}

	return ciphertext, nil
}

Can this be made possible via the exposed functions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant