I am trying to encrypt certain fields in mongodb using mgo and Golang. Since mongodb does not support field level encryption , I was thinking of encrypting and decrypting the data while marshalling the data and unmarshalling the data.
For example, in the struct below I want to encrypt Name and Gender
if my struct as like this
type User struct {
UserID string `json:"userID,omitempty" bson:"userID,omitempty"`
UserName string `json:"userName,omitempty" bson:"userName,omitempty"`
UserAge string `json:"userAge,omitempty" bson:"userAge,omitempty"`
UserGender string `json:"userGender,omitempty" bson:"userGender,omitempty"`
}
One way to solve this is after marshalling the data, encrypt the data before saving in the db, and a similar approach for sending the data back to the UI. Is there an event that I can listen to before the marshalling and unmarshalling of data? or is there a better way to do it?