mirror of https://github.com/schollz/croc.git
add box for bundling
This commit is contained in:
parent
65bbee501c
commit
a54ab1118c
1
go.mod
1
go.mod
|
@ -7,6 +7,7 @@ require (
|
|||
github.com/gin-contrib/cors v1.3.0
|
||||
github.com/gin-gonic/gin v1.4.0
|
||||
github.com/gorilla/websocket v1.4.1
|
||||
github.com/json-iterator/go v1.1.6
|
||||
github.com/pion/webrtc/v2 v2.1.12
|
||||
github.com/pions/webrtc v1.2.0
|
||||
github.com/pkg/errors v0.8.1
|
||||
|
|
3
go.sum
3
go.sum
|
@ -29,6 +29,7 @@ github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
|
|||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/kalafut/imohash v1.0.0 h1:LgCJ+p/BwM2HKpOxFopkeddpzVCfm15EtXMroXD1SYE=
|
||||
github.com/kalafut/imohash v1.0.0/go.mod h1:c3RHT80ZAp5C/aYgQI92ZlrOymqkZnRDprU87kg75HI=
|
||||
|
@ -46,7 +47,9 @@ github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg
|
|||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package box
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/schollz/croc/v7/src/compress"
|
||||
"github.com/schollz/croc/v7/src/crypt"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
// Bundle will marshal, encrypt, and compress some data into a base64 string
|
||||
func Bundle(payload interface{}, key []byte) (bundled string, err error) {
|
||||
p, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p = compress.Compress(p)
|
||||
if key != nil {
|
||||
p, err = crypt.Encrypt(p, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
// TODO: use base-122 encoding instead? https://github.com/kevinAlbs/Base122
|
||||
bundled = base64.StdEncoding.EncodeToString(p)
|
||||
return
|
||||
}
|
||||
|
||||
// Unbundle will decode, decrypt, and decompress the payload into the interface
|
||||
func Unbundle(bundled string, key []byte, payload interface{}) (err error) {
|
||||
b, err := base64.StdEncoding.DecodeString(bundled)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if key != nil {
|
||||
b, err = crypt.Decrypt(b, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
b = compress.Decompress(b)
|
||||
err = json.Unmarshal(b, &payload)
|
||||
return
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package box
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/schollz/croc/v7/src/crypt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type M struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func BenchmarkBundle(b *testing.B) {
|
||||
key, _, _ := crypt.New([]byte("password"), nil)
|
||||
for i := 0; i < b.N; i++ {
|
||||
Bundle(M{"hello, world"}, key)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUnbundle(b *testing.B) {
|
||||
key, _, _ := crypt.New([]byte("password"), nil)
|
||||
bundled, _ := Bundle(M{"hello, world"}, key)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
var m M
|
||||
Unbundle(bundled, key, &m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBox(t *testing.T) {
|
||||
key, _, _ := crypt.New([]byte("password"), nil)
|
||||
|
||||
bundled, err := Bundle(M{"hello, world"}, key)
|
||||
assert.Nil(t, err)
|
||||
|
||||
var m M
|
||||
err = Unbundle(bundled, key, &m)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "hello, world", m.Message)
|
||||
}
|
Loading…
Reference in New Issue