Skip to content

Commit

Permalink
feat: Offer non-cloning options for non-mobile clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lubux committed Dec 8, 2023
1 parent 7e59772 commit 2108fcd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ func NewKeyFromReaderExplicit(r io.Reader, encoding int8) (key *Key, err error)
}

// NewKey creates a new key from the first key in the unarmored or armored binary data.
// Clones the binKeys data for go-mobile compatibility.
func NewKey(binKeys []byte) (key *Key, err error) {
return NewKeyFromReader(bytes.NewReader(clone(binKeys)))
}

// NewKeyWithCloneFlag creates a new key from the first key in the unarmored or armored binary data.
func NewKeyWithCloneFlag(binKeys []byte, clone bool) (key *Key, err error) {
if clone {
return NewKey(binKeys)
}
return NewKeyFromReader(bytes.NewReader(binKeys))
}

// NewKeyFromArmored creates a new key from the first key in an armored string.
func NewKeyFromArmored(armored string) (key *Key, err error) {
return NewKeyFromReader(strings.NewReader(armored))
Expand Down
15 changes: 13 additions & 2 deletions crypto/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,25 @@ func NewMetadata(isUTF8 bool) *LiteralMetadata {
}

// NewPGPMessage generates a new PGPMessage from the unarmored binary data.
// Clones the data for go-mobile compatibility.
func NewPGPMessage(data []byte) *PGPMessage {
return NewPGPMessageWithCloneFlag(data, true)
}

// NewPGPMessageWithCloneFlag generates a new PGPMessage from the unarmored binary data.
func NewPGPMessageWithCloneFlag(data []byte, doClone bool) *PGPMessage {
packetData := data
if doClone {
packetData = clone(data)
}
pgpMessage := &PGPMessage{
DataPacket: clone(data),
DataPacket: packetData,
}
pgpMessage, err := pgpMessage.splitMessage()
if err != nil {
// If there is an error in split treat the data as data packets.
return &PGPMessage{
DataPacket: clone(data),
DataPacket: packetData,
}
}
return pgpMessage
Expand Down Expand Up @@ -92,6 +102,7 @@ func NewPGPMessageFromArmored(armored string) (*PGPMessage, error) {
}

// NewPGPSplitMessage generates a new PGPSplitMessage from the binary unarmored keypacket and datapacket.
// Clones the slices for go-mobile compatibility.
func NewPGPSplitMessage(keyPacket []byte, dataPacket []byte) *PGPMessage {
return &PGPMessage{
KeyPacket: clone(keyPacket),
Expand Down
2 changes: 2 additions & 0 deletions crypto/sessionkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func generateSessionKey(config *packet.Config) (*SessionKey, error) {
return GenerateSessionKeyAlgo(cf)
}

// NewSessionKeyFromToken creates a SessionKey struct with the given token and algorithm.
// Clones the token for compatibility with go-mobile.
func NewSessionKeyFromToken(token []byte, algo string) *SessionKey {
return &SessionKey{
Key: clone(token),
Expand Down

0 comments on commit 2108fcd

Please sign in to comment.