@@ -12,26 +12,33 @@ import (
1212 "strconv"
1313)
1414
15+ // TableColumn represents the table and column an encrypted value belongs to
1516type TableColumn struct {
1617 T string `json:"t"`
1718 C string `json:"c"`
1819}
1920
21+ // EncryptedColumn represents the plaintext value sent by a database client
2022type EncryptedColumn struct {
2123 K string `json:"k"`
2224 P string `json:"p"`
2325 I TableColumn `json:"i"`
2426 V int `json:"v"`
2527}
2628
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
2930type EncryptedText string
31+
32+ // EncryptedJsonb is a jsonb value to be encrypted
3033type EncryptedJsonb map [string ]interface {}
34+
35+ // EncryptedInt is a int value to be encrypted
3136type EncryptedInt int
37+
38+ // EncryptedBool is a bool value to be encrypted
3239type EncryptedBool bool
3340
34- // Text
41+ // Serialize turns a EncryptedText value into a jsonb payload for CipherStash Proxy
3542func (et EncryptedText ) Serialize (table string , column string ) ([]byte , error ) {
3643 val , err := ToEncryptedColumn (string (et ), table , column )
3744 if err != nil {
@@ -40,6 +47,7 @@ func (et EncryptedText) Serialize(table string, column string) ([]byte, error) {
4047 return json .Marshal (val )
4148}
4249
50+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedText value
4351func (et * EncryptedText ) Deserialize (data []byte ) (EncryptedText , error ) {
4452 var jsonData map [string ]interface {}
4553 if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -53,7 +61,7 @@ func (et *EncryptedText) Deserialize(data []byte) (EncryptedText, error) {
5361 return "" , fmt .Errorf ("invalid format: missing 'p' field in JSONB" )
5462}
5563
56- // Jsonb
64+ // Serialize turns a EncryptedJsonb value into a jsonb payload for CipherStash Proxy
5765func (ej EncryptedJsonb ) Serialize (table string , column string ) ([]byte , error ) {
5866 val , err := ToEncryptedColumn (map [string ]any (ej ), table , column )
5967 if err != nil {
@@ -62,6 +70,7 @@ func (ej EncryptedJsonb) Serialize(table string, column string) ([]byte, error)
6270 return json .Marshal (val )
6371}
6472
73+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedJsonb value
6574func (ej * EncryptedJsonb ) Deserialize (data []byte ) (EncryptedJsonb , error ) {
6675 var jsonData map [string ]interface {}
6776 if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -80,7 +89,7 @@ func (ej *EncryptedJsonb) Deserialize(data []byte) (EncryptedJsonb, error) {
8089 return nil , fmt .Errorf ("invalid format: missing 'p' field in JSONB" )
8190}
8291
83- // Int
92+ // Serialize turns a EncryptedInt value into a jsonb payload for CipherStash Proxy
8493func (et EncryptedInt ) Serialize (table string , column string ) ([]byte , error ) {
8594 val , err := ToEncryptedColumn (int (et ), table , column )
8695 if err != nil {
@@ -89,6 +98,7 @@ func (et EncryptedInt) Serialize(table string, column string) ([]byte, error) {
8998 return json .Marshal (val )
9099}
91100
101+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedInt value
92102func (et * EncryptedInt ) Deserialize (data []byte ) (EncryptedInt , error ) {
93103 var jsonData map [string ]interface {}
94104 if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -106,7 +116,7 @@ func (et *EncryptedInt) Deserialize(data []byte) (EncryptedInt, error) {
106116 return 0 , fmt .Errorf ("invalid format: missing 'p' field" )
107117}
108118
109- // Bool
119+ // Serialize turns a EncryptedBool value into a jsonb payload for CipherStash Proxy
110120func (eb EncryptedBool ) Serialize (table string , column string ) ([]byte , error ) {
111121 val , err := ToEncryptedColumn (bool (eb ), table , column )
112122 if err != nil {
@@ -115,6 +125,7 @@ func (eb EncryptedBool) Serialize(table string, column string) ([]byte, error) {
115125 return json .Marshal (val )
116126}
117127
128+ // Deserialize turns a jsonb payload from CipherStash Proxy into an EncryptedBool value
118129func (et * EncryptedBool ) Deserialize (data []byte ) (EncryptedBool , error ) {
119130 var jsonData map [string ]interface {}
120131 if err := json .Unmarshal (data , & jsonData ); err != nil {
@@ -133,8 +144,7 @@ func (et *EncryptedBool) Deserialize(data []byte) (EncryptedBool, error) {
133144 return false , fmt .Errorf ("invalid format: missing 'p' field" )
134145}
135146
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.
138148func SerializeQuery (value any , table string , column string ) ([]byte , error ) {
139149 query , err := ToEncryptedColumn (value , table , column )
140150 if err != nil {
@@ -149,7 +159,7 @@ func SerializeQuery(value any, table string, column string) ([]byte, error) {
149159
150160}
151161
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 .
153163func ToEncryptedColumn (value any , table string , column string ) (EncryptedColumn , error ) {
154164 str , err := convertToString (value )
155165 if err != nil {
0 commit comments