Encrypt and decrypt JSON with secretbox.
package main
import (
"encoding/json"
"github.com/zemirco/jsonbox"
)
type Person struct {
Name string `json:"name"`
// use jsonbox.Secret as type (string is underlying type)
Code jsonbox.Secret `json:"code"`
}
func main() {
// use your own key of length 32 in production
rand.Reader.Read(jsonbox.Key[:])
// create new instance
john := Person{
Name: "john",
Code: "open sesame",
}
res, err := json.Marshal(john)
if err != nil {
panic(err)
}
// make sure code field is encrypted
log.Println(string(res))
// unmarshal json into new struct
steve := Person{}
err = json.Unmarshal(res, &steve)
if err != nil {
panic(err)
}
log.Printf("%+v", steve)
}
go test
MIT