-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add image+audio support + refactory group
- Loading branch information
Showing
6 changed files
with
244 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package o3 | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
// MsgType mock enum | ||
const MessageTypeAudio MsgType = 0x14 | ||
|
||
//AudioMessage represents a image message as sent e2e encrypted to other threema users | ||
type AudioMessage struct { | ||
*MessageHeader | ||
Duration uint16 | ||
BlobID [16]byte | ||
ServerID byte | ||
Size uint32 | ||
Key [32]byte | ||
} | ||
|
||
// String returns the message text as string | ||
func (m AudioMessage) String() string { | ||
return fmt.Sprintf("AudioMSG: https://%2x.blob.threema.ch/%16x, Size: %d, Key: %24x", m.ServerID, m.BlobID, m.Size, m.Key) | ||
} | ||
|
||
// GetData return the decrypted Audio needs the recipients secret key | ||
func (m AudioMessage) GetData() ([]byte, error) { | ||
return downloadAndDecryptSym(m.BlobID, m.Key) | ||
} | ||
|
||
// SetAudio encrypts and uploads the image by file. Sets the blob info in the AudioMessage. Needs the recipients public key. | ||
func (m *AudioMessage) SetDataByFile(filename string) error { | ||
data, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return errors.New("could not load image") | ||
} | ||
return m.SetData(data) | ||
} | ||
|
||
// SetAudioData encrypts and uploads the image. Sets the blob info in the AudioMessage. Needs the recipients public key. | ||
func (m *AudioMessage) SetData(data []byte) (err error) { | ||
// TODO: Should we have a whole media lib as dependency just to set this to the proper value? | ||
m.Duration = 0xFF | ||
m.Key, m.ServerID, m.Size, m.BlobID, err = encryptSymAndUpload(data) | ||
return | ||
} | ||
|
||
//Serialize returns a fully serialized byte slice of a TextMessage | ||
func (m AudioMessage) MarshalBinary() ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
bufMarshal("msg-type", buf, MessageTypeAudio) | ||
bufMarshal("duration", buf, 0xFFFF) | ||
bufMarshal("blob-id", buf, m.BlobID) | ||
bufMarshal("size", buf, m.Size) | ||
bufMarshal("nonce", buf, m.Key) | ||
bufMarshalPadding(buf) | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func (m *AudioMessage) UnmarshalBinary(data []byte) error { | ||
buf := bytes.NewBuffer(data) | ||
var t MsgType | ||
bufUnmarshal("read message type", buf, &t) | ||
if t != MessageTypeText { | ||
return errors.New("not correct type") | ||
} | ||
stripPadding(buf) | ||
|
||
bufUnmarshal("duration", buf, &m.Duration) | ||
bufUnmarshal("blob-id", buf, &m.BlobID) | ||
bufUnmarshal("size", buf, &m.Size) | ||
bufUnmarshal("key", buf, &m.Key) | ||
|
||
m.ServerID = m.BlobID[0] | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
messageUnmarshal[MessageTypeAudio] = func(mh *MessageHeader, data []byte) (Message, error) { | ||
m := &AudioMessage{ | ||
MessageHeader: mh, | ||
} | ||
if err := m.UnmarshalBinary(data); err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package o3 | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
// MsgType mock enum | ||
const MessageTypeImage MsgType = 0x2 | ||
|
||
//ImageMessage represents a image message as sent e2e encrypted to other threema users | ||
type ImageMessage struct { | ||
*MessageHeader | ||
BlobID [16]byte | ||
ServerID byte | ||
Size uint32 | ||
Nonce nonce | ||
} | ||
|
||
// String returns the message text as string | ||
func (m ImageMessage) String() string { | ||
return fmt.Sprintf("ImageMSG: https://%2x.blob.threema.ch/%16x, Size: %d, Nonce: %24x", m.ServerID, m.BlobID, m.Size, m.Nonce.nonce) | ||
} | ||
|
||
// GetImageData return the decrypted Image needs the recipients secret key | ||
func (m ImageMessage) GetData(threemaID *ThreemaID) ([]byte, error) { | ||
return downloadAndDecryptAsym(threemaID, m.BlobID, m.Sender.String(), m.Nonce) | ||
} | ||
|
||
// SetDataByFile encrypts and uploads the image by file. Sets the blob info in the ImageMessage. Needs the recipients public key. | ||
func (m *ImageMessage) SetDataByFile(threemaID *ThreemaID, filename string) error { | ||
data, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return errors.New("could not load image") | ||
} | ||
return m.SetData(threemaID, data) | ||
} | ||
|
||
// SetData encrypts and uploads the image. Sets the blob info in the ImageMessage. Needs the recipients public key. | ||
func (m *ImageMessage) SetData(threemaID *ThreemaID, data []byte) (err error) { | ||
m.Nonce, m.ServerID, m.Size, m.BlobID, err = encryptAsymAndUpload(threemaID, data, m.Recipient.String()) | ||
return | ||
} | ||
|
||
//Serialize returns a fully serialized byte slice of a TextMessage | ||
func (m ImageMessage) MarshalBinary() ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
bufMarshal("msg-type", buf, MessageTypeImage) | ||
bufMarshal("blob-id", buf, m.BlobID) | ||
bufMarshal("size", buf, m.Size) | ||
bufMarshal("nonce", buf, m.Nonce.nonce) | ||
bufMarshalPadding(buf) | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func (m *ImageMessage) UnmarshalBinary(data []byte) error { | ||
buf := bytes.NewBuffer(data) | ||
var t MsgType | ||
bufUnmarshal("read message type", buf, &t) | ||
if t != MessageTypeText { | ||
return errors.New("not correct type") | ||
} | ||
stripPadding(buf) | ||
|
||
bufUnmarshal("blob-id", buf, &m.BlobID) | ||
bufUnmarshal("size", buf, &m.Size) | ||
bufUnmarshal("nonce", buf, &m.Nonce.nonce) | ||
|
||
m.ServerID = m.BlobID[0] | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
messageUnmarshal[MessageTypeImage] = func(mh *MessageHeader, data []byte) (Message, error) { | ||
m := &ImageMessage{ | ||
MessageHeader: mh, | ||
} | ||
if err := m.UnmarshalBinary(data); err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package o3 | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type GroupMessageHeader struct { | ||
CreatorID IDString | ||
GroupID [8]byte | ||
} | ||
|
||
const GroupMessageHeaderLenght = 16 | ||
|
||
func (msg GroupMessageHeader) MarshalBinary() ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
bufMarshal("gh-creator id", buf, msg.CreatorID) | ||
bufMarshal("gh-group id", buf, msg.GroupID) | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (msg *GroupMessageHeader) UnmarshalBinary(data []byte) error { | ||
buf := bytes.NewBuffer(data) | ||
bufUnmarshal("read group creator", buf, &msg.CreatorID) | ||
bufUnmarshal("read group id", buf, &msg.GroupID) | ||
|
||
return nil | ||
} |
Oops, something went wrong.