-
Notifications
You must be signed in to change notification settings - Fork 2
/
protoCrypt.go
55 lines (52 loc) · 1.9 KB
/
protoCrypt.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package protocrypt
import "github.com/golang/protobuf/proto"
// ProtoCrypt is a interface to a Protobuf Field Encrypter / Decrypter
//
// Encryption usage:
// // Assuming data is a byte slice with the original protobuf in wire format
// pc := protocrypt.New(data)
//
// key, err := protocrypt.GenerateKey()
// if err != nil {
// panic(err)
// }
//
// err = pc.EncryptFields([]uint{1,3}, key)
// if err != nil {
// panic(err)
// }
//
// data = pc.Serialize()
// // data now contains encrypted fields 1 and 3 with the specified key
//
// Decryption usage:
// pc = protocrypt.New(data)
//
// y := &sample.TestMessage{}
//
// err = pc.DecryptAndUnmarshal([]uint{1,3}, key, y)
// if err != nil {
// panic(err)
// }
//
// // Now y is filled with decrypted data
type ProtoCrypt interface {
// EncryptFields encrypts the specified fields with the specified key
// If you call more than once, it will encrypt the already encrypted payload
EncryptFields(fieldsToEncrypt []uint, key []byte) error
// DecryptFields decrypts the specified fields with the specified key
// If you call more than once, it will try to decrypt a decrypted content
DecryptFields(fieldsToDecrypt []uint, key []byte) error
// Serialize serializes the protobuf to protobuf wire format (compatible with proto.Marshal / Unmarshal
Serialize() []byte
// Unmarshal unmarshals the protocrypt loaded data into a protobuf message
// Equivalent to call proto.Unmarshal(pc.Serialize(), pb)
Unmarshal(pb proto.Message) error
// DecryptAndUnmarshal decrypts and unmarshals protocrypt loaded data into a protobuf message
// Equivalent to call DecryptFilds then Unmarshal
DecryptAndUnmarshal(fieldsToEncrypt []uint, key []byte, pb proto.Message) error
}
// New returns a instance of ProtoCrypt for the specified protobuf wire binary data
func New(data []byte) ProtoCrypt {
return parseProto(data)
}