Move encrypt detached helper to avoid building on mobile (#79)
This commit is contained in:
parent
55c4bd994d
commit
16024c21ec
2 changed files with 22 additions and 22 deletions
79
helper/sign_detached.go
Normal file
79
helper/sign_detached.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// +build !ios
|
||||
// +build !android
|
||||
|
||||
package helper
|
||||
|
||||
import "github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
|
||||
// EncryptSignAttachment encrypts an attachment using a detached signature, given a publicKey, a privateKey
|
||||
// and its passphrase, the filename, and the unencrypted file data.
|
||||
// Returns keypacket, dataPacket and unarmored (!) signature separate.
|
||||
func EncryptSignAttachment(
|
||||
publicKey, privateKey string, passphrase []byte, fileName string, plainData []byte,
|
||||
) (keyPacket, dataPacket, signature []byte, err error) {
|
||||
var publicKeyObj, privateKeyObj, unlockedKeyObj *crypto.Key
|
||||
var publicKeyRing, privateKeyRing *crypto.KeyRing
|
||||
var packets *crypto.PGPSplitMessage
|
||||
var signatureObj *crypto.PGPSignature
|
||||
|
||||
var binMessage = crypto.NewPlainMessage(plainData)
|
||||
|
||||
if publicKeyObj, err = crypto.NewKeyFromArmored(publicKey); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
if publicKeyObj.IsPrivate() {
|
||||
publicKeyObj, err = publicKeyObj.ToPublic()
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if publicKeyRing, err = crypto.NewKeyRing(publicKeyObj); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
defer unlockedKeyObj.ClearPrivateParams()
|
||||
|
||||
if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
if packets, err = publicKeyRing.EncryptAttachment(binMessage, fileName); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
if signatureObj, err = privateKeyRing.SignDetached(binMessage); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
return packets.GetBinaryKeyPacket(), packets.GetBinaryDataPacket(), signatureObj.GetBinary(), nil
|
||||
}
|
||||
|
||||
// EncryptSignArmoredDetached takes a public key for encryption,
|
||||
// a private key and its passphrase for signature, and the plaintext data
|
||||
// Returns an armored ciphertext and a detached armored signature.
|
||||
func EncryptSignArmoredDetached(
|
||||
publicKey, privateKey string,
|
||||
passphrase, plainData []byte,
|
||||
) (ciphertext, signature string, err error) {
|
||||
var message *crypto.PlainMessage = crypto.NewPlainMessage(plainData)
|
||||
|
||||
// We encrypt the message
|
||||
if ciphertext, err = encryptMessageArmored(publicKey, message); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// We sign the message
|
||||
if signature, err = signDetachedArmored(privateKey, passphrase, message); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return ciphertext, signature, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue