Documentation
¶
Overview ¶
Package cryptowrap JSON/Gob/MsgPack-based Marshaler/Unmarshaler with AES encryption
Package cryptowrap JSON/Gob/MsgPack-based Marshaler/Unmarshaler with AES encryption
Example (Cbor) ¶
type toPass struct {
Insecure string
Secure cryptowrap.Wrapper
}
type toPassSecure struct {
Field string
}
key := []byte("0123456789ABCDEF")
srcSecure := toPassSecure{"hello world!"}
src := toPass{
Insecure: "hello",
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &srcSecure,
},
}
data, err := cbor.Marshal(&src)
if err != nil {
panic(err)
}
var dstSecure toPassSecure
dst := toPass{
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &dstSecure,
},
}
err = cbor.Unmarshal(data, &dst)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", dst.Secure.Payload.(*toPassSecure).Field)
Output: hello world!
Example (Direct) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/Djarvur/cryptowrap"
)
func main() {
key := []byte("0123456789ABCDEF")
src := "hello!"
data, err := json.Marshal(&cryptowrap.Wrapper{Keys: [][]byte{key}, Payload: &src})
if err != nil {
panic(err)
}
// var onTheGo interface{}
//
// err = json.Unmarshal(data, &onTheGo)
// if err != nil {
// panic(err)
// }
//
// log.Printf("payload is encrypted: %v\n", onTheGo)
var dst string
err = json.Unmarshal(data, &cryptowrap.Wrapper{Keys: [][]byte{key}, Payload: &dst})
if err != nil {
panic(err)
}
fmt.Printf("%v\n", dst)
}
Output: hello!
Example (Embeded) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/Djarvur/cryptowrap"
)
func main() {
type toPass struct {
Insecure string
Secure cryptowrap.Wrapper
}
type toPassSecure struct {
Field string
}
key := []byte("0123456789ABCDEF")
srcSecure := toPassSecure{"world!"}
src := toPass{
Insecure: "hello",
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &srcSecure,
},
}
data, err := json.Marshal(&src)
if err != nil {
panic(err)
}
// var onTheGo interface{}
//
// err = json.Unmarshal(data, &onTheGo)
// if err != nil {
// panic(err)
// }
//
// log.Printf("payload is encrypted: %v\n", onTheGo)
var dstSecure toPassSecure
dst := toPass{
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &dstSecure,
},
}
err = json.Unmarshal(data, &dst)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", dst.Secure.Payload.(*toPassSecure).Field)
}
Output: world!
Example (Gob) ¶
package main
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/Djarvur/cryptowrap"
)
func main() {
type toPass struct {
Insecure string
Secure cryptowrap.Wrapper
}
type toPassSecure struct {
Field string
}
key := []byte("0123456789ABCDEF")
srcSecure := toPassSecure{"hello world!"}
src := toPass{
Insecure: "hello",
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &srcSecure,
},
}
gob.Register(&srcSecure)
var b bytes.Buffer
err := gob.NewEncoder(&b).Encode(&src)
if err != nil {
panic(err)
}
data := b.Bytes()
var dstSecure toPassSecure
dst := toPass{
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &dstSecure,
},
}
err = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&dst)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", dst.Secure.Payload.(*toPassSecure).Field)
}
Output: hello world!
Example (Msgpack) ¶
package main
import (
"bytes"
"fmt"
"github.com/Djarvur/cryptowrap"
"github.com/ugorji/go/codec"
)
func main() {
type toPass struct {
Insecure string
Secure cryptowrap.Wrapper
}
type toPassSecure struct {
Field string
}
key := []byte("0123456789ABCDEF")
srcSecure := toPassSecure{"hello world!"}
src := toPass{
Insecure: "hello",
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &srcSecure,
},
}
var b bytes.Buffer
err := codec.NewEncoder(&b, new(codec.MsgpackHandle)).Encode(&src)
if err != nil {
panic(err)
}
data := b.Bytes()
var dstSecure toPassSecure
dst := toPass{
Secure: cryptowrap.Wrapper{
Keys: [][]byte{key},
Payload: &dstSecure,
},
}
err = codec.NewDecoderBytes(data, new(codec.MsgpackHandle)).Decode(&dst)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", dst.Secure.Payload.(*toPassSecure).Field)
}
Output: hello world!
Example (Rsa) ¶
package main
import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"github.com/Djarvur/cryptowrap"
)
func main() {
type toPass struct {
Insecure string
Secure cryptowrap.WrapperRSA
}
type toPassSecure struct {
Field string
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
srcSecure := toPassSecure{"world!"}
src := toPass{
Insecure: "hello",
Secure: cryptowrap.WrapperRSA{
EncKey: &key.PublicKey,
Payload: &srcSecure,
},
}
data, err := json.Marshal(&src)
if err != nil {
panic(err)
}
var dstSecure toPassSecure
dst := toPass{
Secure: cryptowrap.WrapperRSA{
DecKeys: []*rsa.PrivateKey{key},
Payload: &dstSecure,
},
}
err = json.Unmarshal(data, &dst)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", dst.Secure.Payload.(*toPassSecure).Field)
}
Output: world!
Index ¶
- Variables
- type Wrapper
- func (w *Wrapper) GobDecode(data []byte) error
- func (w *Wrapper) GobEncode() ([]byte, error)
- func (w *Wrapper) MarshalBinary() (data []byte, err error)
- func (w *Wrapper) MarshalJSON() ([]byte, error)
- func (w *Wrapper) UnmarshalBinary(data []byte) error
- func (w *Wrapper) UnmarshalJSON(data []byte) error
- type WrapperRSA
- func (w *WrapperRSA) GobDecode(data []byte) error
- func (w *WrapperRSA) GobEncode() ([]byte, error)
- func (w *WrapperRSA) MarshalBinary() (data []byte, err error)
- func (w *WrapperRSA) MarshalJSON() ([]byte, error)
- func (w *WrapperRSA) UnmarshalBinary(data []byte) error
- func (w *WrapperRSA) UnmarshalJSON(data []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUndecryptable = errors.New("data could not be decrypted") ErrNoKey = errors.New("key has to be provided") )
Errors might be returned. They will be wrapped with stacktrace at least, of course.
Functions ¶
This section is empty.
Types ¶
type Wrapper ¶
Wrapper is a struct with custom JSON/Gob/Binary marshaler and unmarshaler.
Marshaler will encrypt Payload with AES using first value from Keys as a key and provided IV as an initialisation vector. Random string will be used if no IV provided.
Actual AES form will be chosen based on first Keys value length.
Serialised data are protected by checksum.
Unmarshaler will decrypt Payload with the Keys provided. Keys will be tryied one by one until success decryption. Success means checksum check satisfied. ErrUndecryptable will be returned in case no one key is suitable.
If Compress is true serialized Payload wil be compressed with LZ4.
func (*Wrapper) MarshalBinary ¶ added in v0.1.1
MarshalBinary is a custom marshaler to be used with MsgPack (github.com/ugorji/go/codec).
func (*Wrapper) MarshalJSON ¶
MarshalJSON is a custom marshaler.
func (*Wrapper) UnmarshalBinary ¶ added in v0.1.1
UnmarshalBinary is a custom unmarshaler to be used with MsgPack (github.com/ugorji/go/codec).
func (*Wrapper) UnmarshalJSON ¶
UnmarshalJSON is a custom unmarshaler.
type WrapperRSA ¶ added in v0.2.0
type WrapperRSA struct {
DecKeys []*rsa.PrivateKey
EncKey *rsa.PublicKey
Hash hash.Hash
Label []byte
Payload interface{}
Compress bool
}
WrapperRSA is a struct with custom JSON/Gob/Binary marshaler and unmarshaler.
Marshaler will encrypt Payload with RSA using EncKey as a public key. and hash function provided in Hash. sha256.New() will be used if no Hash provided.
Unmarshaler will decrypt Payload with the DecKeys provided. Keys will be tryied one by one until success decryption. ErrUndecryptable will be returned in case no one key is suitable.
Label must be the same for Marshaling and Umarshaling. If no label provided empty one is used.
If Compress is true serialized Payload wil be compressed with LZ4.
Note: there is a limit for the length of data could be encrypted with RSA: The message must be no longer than the length of the public modulus minus twice the hash length, minus a further 2. See https://golang.org/pkg/crypto/rsa/#EncryptOAEP for details (there no much though).
func (*WrapperRSA) GobDecode ¶ added in v0.2.0
func (w *WrapperRSA) GobDecode(data []byte) error
GobDecode is a custom unmarshaler.
func (*WrapperRSA) GobEncode ¶ added in v0.2.0
func (w *WrapperRSA) GobEncode() ([]byte, error)
GobEncode is a custom marshaler.
func (*WrapperRSA) MarshalBinary ¶ added in v0.2.0
func (w *WrapperRSA) MarshalBinary() (data []byte, err error)
MarshalBinary is a custom marshaler to be used with MsgPack (github.com/ugorji/go/codec).
func (*WrapperRSA) MarshalJSON ¶ added in v0.2.0
func (w *WrapperRSA) MarshalJSON() ([]byte, error)
MarshalJSON is a custom marshaler.
func (*WrapperRSA) UnmarshalBinary ¶ added in v0.2.0
func (w *WrapperRSA) UnmarshalBinary(data []byte) error
UnmarshalBinary is a custom unmarshaler to be used with MsgPack (github.com/ugorji/go/codec).
func (*WrapperRSA) UnmarshalJSON ¶ added in v0.2.0
func (w *WrapperRSA) UnmarshalJSON(data []byte) error
UnmarshalJSON is a custom unmarshaler.