@@ -29,44 +29,10 @@ type EncryptedColumn struct {
29
29
P string `json:"p"`
30
30
I TableColumn `json:"i"`
31
31
V int `json:"v"`
32
+ Q any `json:"q"`
32
33
}
33
34
34
35
// EncryptedText is a string value to be encrypted
35
- // def for_match(value)
36
- // for_query(value, "match")
37
- // end
38
-
39
- // def for_ore(value)
40
- // for_query(value, "ore")
41
- // end
42
-
43
- // def for_unique(value)
44
- // for_query(value, "unique")
45
- // end
46
-
47
- // def for_ste_vec(value)
48
- // for_query(value, "ste_vec")
49
- // end
50
-
51
- // def for_query(value, for_query)
52
- // eql_payload(value, for_query).to_json()
53
- // end
54
-
55
- // def eql_payload(value, for_query)
56
- // {
57
- // k: "pt",
58
- // p: serialize_plaintext_value(value),
59
- // i: {
60
- // t: table,
61
- // c: column
62
- // },
63
- // v: 1,
64
- // q: for_query,
65
- // }
66
- // end
67
- //
68
- // Creating custom types for encrypted fields to enable creating methods for
69
- // serialization/deserialization of these types.
70
36
type EncryptedText string
71
37
72
38
// EncryptedJsonb is a jsonb value to be encrypted
@@ -80,7 +46,7 @@ type EncryptedBool bool
80
46
81
47
// Serialize turns a EncryptedText value into a jsonb payload for CipherStash Proxy
82
48
func (et EncryptedText ) Serialize (table string , column string ) ([]byte , error ) {
83
- val , err := ToEncryptedColumn (string (et ), table , column )
49
+ val , err := ToEncryptedColumn (string (et ), table , column , nil )
84
50
if err != nil {
85
51
return nil , fmt .Errorf ("error serializing: %v" , err )
86
52
}
@@ -103,7 +69,7 @@ func (et *EncryptedText) Deserialize(data []byte) (EncryptedText, error) {
103
69
104
70
// Serialize turns a EncryptedJsonb value into a jsonb payload for CipherStash Proxy
105
71
func (ej EncryptedJsonb ) Serialize (table string , column string ) ([]byte , error ) {
106
- val , err := ToEncryptedColumn (map [string ]any (ej ), table , column )
72
+ val , err := ToEncryptedColumn (map [string ]any (ej ), table , column , nil )
107
73
if err != nil {
108
74
return nil , fmt .Errorf ("error serializing: %v" , err )
109
75
}
@@ -131,7 +97,7 @@ func (ej *EncryptedJsonb) Deserialize(data []byte) (EncryptedJsonb, error) {
131
97
132
98
// Serialize turns a EncryptedInt value into a jsonb payload for CipherStash Proxy
133
99
func (et EncryptedInt ) Serialize (table string , column string ) ([]byte , error ) {
134
- val , err := ToEncryptedColumn (int (et ), table , column )
100
+ val , err := ToEncryptedColumn (int (et ), table , column , nil )
135
101
if err != nil {
136
102
return nil , fmt .Errorf ("error serializing: %v" , err )
137
103
}
@@ -158,7 +124,7 @@ func (et *EncryptedInt) Deserialize(data []byte) (EncryptedInt, error) {
158
124
159
125
// Serialize turns a EncryptedBool value into a jsonb payload for CipherStash Proxy
160
126
func (eb EncryptedBool ) Serialize (table string , column string ) ([]byte , error ) {
161
- val , err := ToEncryptedColumn (bool (eb ), table , column )
127
+ val , err := ToEncryptedColumn (bool (eb ), table , column , nil )
162
128
if err != nil {
163
129
return nil , fmt .Errorf ("error serializing: %v" , err )
164
130
}
@@ -184,9 +150,22 @@ func (eb *EncryptedBool) Deserialize(data []byte) (EncryptedBool, error) {
184
150
return false , fmt .Errorf ("invalid format: missing 'p' field" )
185
151
}
186
152
153
+ func SerializeMatchQuery (value any , table string , column string ) ([]byte , error ) {
154
+ return SerializeQuery (value , table , column , "match" )
155
+ }
156
+ func SerializeOreQuery (value any , table string , column string ) ([]byte , error ) {
157
+ return SerializeQuery (value , table , column , "ore" )
158
+ }
159
+ func SerializeUniqueQuery (value any , table string , column string ) ([]byte , error ) {
160
+ return SerializeQuery (value , table , column , "unique" )
161
+ }
162
+ func SerializeJsonbQuery (value any , table string , column string ) ([]byte , error ) {
163
+ return SerializeQuery (value , table , column , "ste_vec" )
164
+ }
165
+
187
166
// SerializeQuery produces a jsonb payload used by EQL query functions to perform search operations like equality checks, range queries, and unique constraints.
188
- func SerializeQuery (value any , table string , column string ) ([]byte , error ) {
189
- query , err := ToEncryptedColumn (value , table , column )
167
+ func SerializeQuery (value any , table string , column string , queryType any ) ([]byte , error ) {
168
+ query , err := ToEncryptedColumn (value , table , column , queryType )
190
169
if err != nil {
191
170
return nil , fmt .Errorf ("error converting to EncryptedColumn: %v" , err )
192
171
}
@@ -200,15 +179,26 @@ func SerializeQuery(value any, table string, column string) ([]byte, error) {
200
179
}
201
180
202
181
// ToEncryptedColumn converts a plaintext value to a string, and returns the EncryptedColumn struct for inserting into a database.
203
- func ToEncryptedColumn (value any , table string , column string ) (EncryptedColumn , error ) {
204
- str , err := convertToString (value )
205
- if err != nil {
206
- return EncryptedColumn {}, fmt .Errorf ("error: %v" , err )
207
- }
182
+ func ToEncryptedColumn (value any , table string , column string , queryType any ) (EncryptedColumn , error ) {
183
+ if queryType == nil {
184
+ str , err := convertToString (value )
185
+ if err != nil {
186
+ return EncryptedColumn {}, fmt .Errorf ("error: %v" , err )
187
+ }
208
188
209
- data := EncryptedColumn {K : "pt" , P : str , I : TableColumn {T : table , C : column }, V : 1 }
189
+ data := EncryptedColumn {K : "pt" , P : str , I : TableColumn {T : table , C : column }, V : 1 , Q : nil }
210
190
211
- return data , nil
191
+ return data , nil
192
+ } else {
193
+ str , err := convertToString (value )
194
+ if err != nil {
195
+ return EncryptedColumn {}, fmt .Errorf ("error: %v" , err )
196
+ }
197
+
198
+ data := EncryptedColumn {K : "pt" , P : str , I : TableColumn {T : table , C : column }, V : 1 , Q : queryType }
199
+
200
+ return data , nil
201
+ }
212
202
}
213
203
214
204
func convertToString (value any ) (string , error ) {
0 commit comments