/
encode.go
39 lines (31 loc) · 1.08 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package encode
import (
"bytes"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/open-iot-devices/protobufs/go/openiot"
)
// DecryptAndRead decrypts buffer using encoding from encType,
// then de-serializes all messages using "delimited" approach.
func DecryptAndRead(
buffer *bytes.Buffer, encType openiot.EncryptionType, key []byte, msgs ...proto.Message) error {
switch encType {
case openiot.EncryptionType_PLAIN:
return ReadPlain(buffer, msgs...)
case openiot.EncryptionType_AES_ECB:
return DecryptAndReadECB(buffer, key, msgs...)
}
return fmt.Errorf("Encoding %v is not supported", encType)
}
// WriteAndEncrypt serializes all messages using "delimited"
// approach, then encodes result with encoding from encType.
func WriteAndEncrypt(
buffer *bytes.Buffer, encType openiot.EncryptionType, key []byte, msgs ...proto.Message) error {
switch encType {
case openiot.EncryptionType_PLAIN:
return WritePlain(buffer, msgs...)
case openiot.EncryptionType_AES_ECB:
return WriteAndEncryptECB(buffer, key, msgs...)
}
return fmt.Errorf("Encoding %v is not supported", encType)
}