@@ -12,26 +12,33 @@ import (
12
12
"strconv"
13
13
)
14
14
15
+ // TableColumn represents the table and column an encrypted value belongs to
15
16
type TableColumn struct {
16
17
T string `json:"t"`
17
18
C string `json:"c"`
18
19
}
19
20
21
+ // EncryptedColumn represents the plaintext value sent by a database client
20
22
type EncryptedColumn struct {
21
23
K string `json:"k"`
22
24
P string `json:"p"`
23
25
I TableColumn `json:"i"`
24
26
V int `json:"v"`
25
27
}
26
28
27
- // Creating custom types for encrypted fields to enable creating methods for
28
- // serialization/deserialization of these types.
29
+ // EncryptedText is a string value to be encrypted
29
30
type EncryptedText string
31
+
32
+ // EncryptedJsonb is a jsonb value to be encrypted
30
33
type EncryptedJsonb map [string ]interface {}
34
+
35
+ // EncryptedInt is a int value to be encrypted
31
36
type EncryptedInt int
37
+
38
+ // EncryptedBool is a bool value to be encrypted
32
39
type EncryptedBool bool
33
40
34
- // Text
41
+ // Serialize turns a EncryptedText value into a jsonb payload for CipherStash Proxy
35
42
func (et EncryptedText ) Serialize (table string , column string ) ([]byte , error ) {
36
43
val , err := ToEncryptedColumn (string (et ), table , column )
37
44
if err != nil {
@@ -40,6 +47,7 @@ func (et EncryptedText) Serialize(table string, column string) ([]byte, error) {
40
47
return json .Marshal (val )
41
48
}
42
49
50
+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedText value
43
51
func (et * EncryptedText ) Deserialize (data []byte ) (EncryptedText , error ) {
44
52
var jsonData map [string ]interface {}
45
53
if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -53,7 +61,7 @@ func (et *EncryptedText) Deserialize(data []byte) (EncryptedText, error) {
53
61
return "" , fmt .Errorf ("invalid format: missing 'p' field in JSONB" )
54
62
}
55
63
56
- // Jsonb
64
+ // Serialize turns a EncryptedJsonb value into a jsonb payload for CipherStash Proxy
57
65
func (ej EncryptedJsonb ) Serialize (table string , column string ) ([]byte , error ) {
58
66
val , err := ToEncryptedColumn (map [string ]any (ej ), table , column )
59
67
if err != nil {
@@ -62,6 +70,7 @@ func (ej EncryptedJsonb) Serialize(table string, column string) ([]byte, error)
62
70
return json .Marshal (val )
63
71
}
64
72
73
+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedJsonb value
65
74
func (ej * EncryptedJsonb ) Deserialize (data []byte ) (EncryptedJsonb , error ) {
66
75
var jsonData map [string ]interface {}
67
76
if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -80,7 +89,7 @@ func (ej *EncryptedJsonb) Deserialize(data []byte) (EncryptedJsonb, error) {
80
89
return nil , fmt .Errorf ("invalid format: missing 'p' field in JSONB" )
81
90
}
82
91
83
- // Int
92
+ // Serialize turns a EncryptedInt value into a jsonb payload for CipherStash Proxy
84
93
func (et EncryptedInt ) Serialize (table string , column string ) ([]byte , error ) {
85
94
val , err := ToEncryptedColumn (int (et ), table , column )
86
95
if err != nil {
@@ -89,6 +98,7 @@ func (et EncryptedInt) Serialize(table string, column string) ([]byte, error) {
89
98
return json .Marshal (val )
90
99
}
91
100
101
+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedInt value
92
102
func (et * EncryptedInt ) Deserialize (data []byte ) (EncryptedInt , error ) {
93
103
var jsonData map [string ]interface {}
94
104
if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -106,7 +116,7 @@ func (et *EncryptedInt) Deserialize(data []byte) (EncryptedInt, error) {
106
116
return 0 , fmt .Errorf ("invalid format: missing 'p' field" )
107
117
}
108
118
109
- // Bool
119
+ // Serialize turns a EncryptedBool value into a jsonb payload for CipherStash Proxy
110
120
func (eb EncryptedBool ) Serialize (table string , column string ) ([]byte , error ) {
111
121
val , err := ToEncryptedColumn (bool (eb ), table , column )
112
122
if err != nil {
@@ -115,6 +125,7 @@ func (eb EncryptedBool) Serialize(table string, column string) ([]byte, error) {
115
125
return json .Marshal (val )
116
126
}
117
127
128
+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedBool value
118
129
func (et * EncryptedBool ) Deserialize (data []byte ) (EncryptedBool , error ) {
119
130
var jsonData map [string ]interface {}
120
131
if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -133,8 +144,7 @@ func (et *EncryptedBool) Deserialize(data []byte) (EncryptedBool, error) {
133
144
return false , fmt .Errorf ("invalid format: missing 'p' field" )
134
145
}
135
146
136
- // Serialize a query
137
-
147
+ // SerializeQuery produces a jsonb payload used by EQL query functions to perform search operations like equality checks, range queries, and unique constraints.
138
148
func SerializeQuery (value any , table string , column string ) ([]byte , error ) {
139
149
query , err := ToEncryptedColumn (value , table , column )
140
150
if err != nil {
@@ -149,7 +159,7 @@ func SerializeQuery(value any, table string, column string) ([]byte, error) {
149
159
150
160
}
151
161
152
- // Converts a plaintext value to a string and returns the EncryptedColumn struct to use to insert into the db .
162
+ // ToEncryptedColumn converts a plaintext value to a string, and returns the EncryptedColumn struct for inserting into a database .
153
163
func ToEncryptedColumn (value any , table string , column string ) (EncryptedColumn , error ) {
154
164
str , err := convertToString (value )
155
165
if err != nil {
0 commit comments