repositorypackage
0.1.0
Repository: https://github.com/sbreitf1/go-jcrypt.git
Documentation: pkg.go.dev
# Packages
No description provided by the author
# README
JCrypt
Easily encrypt and decrypt annotated fields on-the-fly during JSON marshalling.
Marshaling
Export any data type in JSON representation as done by json.Marshal
and encrypt certain values:
import (
"fmt"
"github.com/sbreitf1/go-jcrypt"
)
type data struct {
UserName string `json:"username"`
Password string `json:"password" jcrypt:"aes"`
}
func main() {
d := data{"obi wan", "deathstar"}
raw, _ := jcrypt.Marshal(d, &jcrypt.Options{
GetKeyHandler: jcrypt.StaticKey([]byte("secret")),
})
fmt.Println(string(raw))
}
The above example will output something like
{
"username":"obi wan",
"password": {
"mode":"aes",
"data":"-uHW77tqZg8ATOVIApk9Wgh3C78x8NZl4E6xFWOTM-i1YsgKwi5NuGYOYNjg6t0pmBQawjxuRT7qDPyMaoGP1A"
}
}
The jcrypt
annotation causes the password
field to be encrypted using AES. The corresponding encryption key is secret
and is passed as static key.
Unmarshaling
Obtaining the plaintext value from an encrypted JSON representation is also comparable to json.Unmarshal
:
import "github.com/sbreitf1/go-jcrypt"
var jsonInputData = `{"username":"obi wan","password": {"mode":"aes","data":` +
`"-uHW77tqZg8ATOVIApk9Wgh3C78x8NZl4E6xFWOTM-i1YsgKwi5NuGYOYNjg6t0pmBQawjxuRT7qDPyMaoGP1A"}`
type data struct {
UserName string `json:"username"`
Password string `json:"password" jcrypt:"aes"`
}
func main() {
var d data
jcrypt.Unmarshal(jsonInputData, &d, &jcrypt.Options{
GetKeyHandler: jcrypt.StaticKey([]byte("secret")),
})
// d now contains "obi wan" and "deathstar"
}
Missing Features
- marshal / unmarshal maps
- Respect json-annotation options like
omitempty
andstring
- Document GetKey-Callback handler for interactive password input
- Other encryption standards
- Check for encryption / disable and document fallback-mode for unencrypted values in annotated fields
- Auto-encrypt files in fallback-mode
- YAML support