@@ -29,44 +29,10 @@ type EncryptedColumn struct {
2929 P string `json:"p"`
3030 I TableColumn `json:"i"`
3131 V int `json:"v"`
32+ Q any `json:"q"`
3233}
3334
3435// 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.
7036type EncryptedText string
7137
7238// EncryptedJsonb is a jsonb value to be encrypted
@@ -80,7 +46,7 @@ type EncryptedBool bool
8046
8147// Serialize turns a EncryptedText value into a jsonb payload for CipherStash Proxy
8248func (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 )
8450 if err != nil {
8551 return nil , fmt .Errorf ("error serializing: %v" , err )
8652 }
@@ -103,7 +69,7 @@ func (et *EncryptedText) Deserialize(data []byte) (EncryptedText, error) {
10369
10470// Serialize turns a EncryptedJsonb value into a jsonb payload for CipherStash Proxy
10571func (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 )
10773 if err != nil {
10874 return nil , fmt .Errorf ("error serializing: %v" , err )
10975 }
@@ -131,7 +97,7 @@ func (ej *EncryptedJsonb) Deserialize(data []byte) (EncryptedJsonb, error) {
13197
13298// Serialize turns a EncryptedInt value into a jsonb payload for CipherStash Proxy
13399func (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 )
135101 if err != nil {
136102 return nil , fmt .Errorf ("error serializing: %v" , err )
137103 }
@@ -158,7 +124,7 @@ func (et *EncryptedInt) Deserialize(data []byte) (EncryptedInt, error) {
158124
159125// Serialize turns a EncryptedBool value into a jsonb payload for CipherStash Proxy
160126func (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 )
162128 if err != nil {
163129 return nil , fmt .Errorf ("error serializing: %v" , err )
164130 }
@@ -184,9 +150,22 @@ func (eb *EncryptedBool) Deserialize(data []byte) (EncryptedBool, error) {
184150 return false , fmt .Errorf ("invalid format: missing 'p' field" )
185151}
186152
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+
187166// 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 )
190169 if err != nil {
191170 return nil , fmt .Errorf ("error converting to EncryptedColumn: %v" , err )
192171 }
@@ -200,15 +179,26 @@ func SerializeQuery(value any, table string, column string) ([]byte, error) {
200179}
201180
202181// 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+ }
208188
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 }
210190
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+ }
212202}
213203
214204func convertToString (value any ) (string , error ) {
0 commit comments