From d53bd4a35108e3c0e826b3a4d2fa4a7e39113081 Mon Sep 17 00:00:00 2001 From: wussler Date: Wed, 14 Oct 2020 18:28:12 +0200 Subject: [PATCH] Move getters to file excluded from android build (#88) --- crypto/attachment.go | 2 +- crypto/keyring_message.go | 5 ++--- crypto/message.go | 12 ++++-------- crypto/message_getters.go | 13 +++++++++++++ crypto/password.go | 10 ++++------ crypto/sessionkey.go | 4 ++-- 6 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 crypto/message_getters.go diff --git a/crypto/attachment.go b/crypto/attachment.go index 179bacf..6c6034c 100644 --- a/crypto/attachment.go +++ b/crypto/attachment.go @@ -109,7 +109,7 @@ func (keyRing *KeyRing) EncryptAttachment(message *PlainMessage, filename string len(message.GetBinary()), filename, message.IsBinary(), - message.GetTime(), + message.Time, -1, ) if err != nil { diff --git a/crypto/keyring_message.go b/crypto/keyring_message.go index 7818464..6a7dbf5 100644 --- a/crypto/keyring_message.go +++ b/crypto/keyring_message.go @@ -5,7 +5,6 @@ import ( "crypto" "io" "io/ioutil" - "time" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/packet" @@ -86,8 +85,8 @@ func asymmetricEncrypt(plainMessage *PlainMessage, publicKey, privateKey *KeyRin hints := &openpgp.FileHints{ IsBinary: plainMessage.IsBinary(), - FileName: plainMessage.GetFilename(), - ModTime: time.Unix(int64(plainMessage.GetTime()), 0), + FileName: plainMessage.Filename, + ModTime: plainMessage.getFormattedTime(), } if plainMessage.IsBinary() { diff --git a/crypto/message.go b/crypto/message.go index 8e70f7c..1677627 100644 --- a/crypto/message.go +++ b/crypto/message.go @@ -10,6 +10,7 @@ import ( "regexp" "runtime" "strings" + "time" "github.com/ProtonMail/gopenpgp/v2/armor" "github.com/ProtonMail/gopenpgp/v2/constants" @@ -220,14 +221,9 @@ func (msg *PlainMessage) IsBinary() bool { return !msg.TextType } -// GetFilename returns the file name of the message as a string. -func (msg *PlainMessage) GetFilename() string { - return msg.Filename -} - -// GetTime returns the modification time of a file (if provided in the ciphertext). -func (msg *PlainMessage) GetTime() uint32 { - return msg.Time +// getFormattedTime returns the message (latest modification) Time as time.Time. +func (msg *PlainMessage) getFormattedTime() time.Time { + return time.Unix(int64(msg.Time), 0) } // GetBinary returns the unarmored binary content of the message as a []byte. diff --git a/crypto/message_getters.go b/crypto/message_getters.go new file mode 100644 index 0000000..3b2443a --- /dev/null +++ b/crypto/message_getters.go @@ -0,0 +1,13 @@ +// +build !android + +package crypto + +// GetFilename returns the file name of the message as a string. +func (msg *PlainMessage) GetFilename() string { + return msg.Filename +} + +// GetTime returns the modification time of a file (if provided in the ciphertext). +func (msg *PlainMessage) GetTime() uint32 { + return msg.Time +} diff --git a/crypto/password.go b/crypto/password.go index 44d5ec1..6f835f6 100644 --- a/crypto/password.go +++ b/crypto/password.go @@ -3,12 +3,10 @@ package crypto import ( "bytes" "io" - "time" - - "golang.org/x/crypto/openpgp" - "golang.org/x/crypto/openpgp/packet" "github.com/pkg/errors" + "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/packet" ) // EncryptMessageWithPassword encrypts a PlainMessage to PGPMessage with a @@ -115,8 +113,8 @@ func passwordEncrypt(message *PlainMessage, password []byte) ([]byte, error) { hints := &openpgp.FileHints{ IsBinary: message.IsBinary(), - FileName: message.GetFilename(), - ModTime: time.Unix(int64(message.GetTime()), 0), + FileName: message.Filename, + ModTime: message.getFormattedTime(), } encryptWriter, err := openpgp.SymmetricallyEncrypt(&outBuf, password, hints, config) diff --git a/crypto/sessionkey.go b/crypto/sessionkey.go index 4d9a474..5e92e86 100644 --- a/crypto/sessionkey.go +++ b/crypto/sessionkey.go @@ -142,8 +142,8 @@ func (sk *SessionKey) Encrypt(message *PlainMessage) ([]byte, error) { encryptWriter, err = packet.SerializeLiteral( encryptWriter, message.IsBinary(), - message.GetFilename(), - message.GetTime(), + message.Filename, + message.Time, ) if err != nil {